Filter Object List & Bind to Datagrid [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an object (please see code below) which contains a list. This list is currently binded to a datagrid in my WPF application.
What I would like to do is to have two datagrids bounded to my object List. I want the top grid to show all the items that have a Status of true. The bottom datagrid would show all the items that have a Status of false. I want both datagrids to be bound to the same object List. Can someone please send me some example code of how to do this?
C# Code
class OrderBlocks
{
public string setting;
public List<Order> Orders;
}
class Order
{
public double Amount;
public int Name;
public bool Status;
}
XAML Code
<DataGrid DataContext="{Binding OrderBlock}"
Name="dataGridOrders"
ItemsSource="{Binding Orders}"/>

You could create 2 properties in your OrderBlocks class
public List<Order> OrdersWithStatusTrue
{
get { return Orders.Where(x => x.Status); }
}
public List<Order> OrdersWithStatusFalse
{
get { return Orders.Where(x => !x.Status); }
}
and bind the first grid to the first property the second one to other.

Related

different between set function by using string oder List<string> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I changed a data structure in my program. I want to have a List instead of a string. Now I have some trouble with the set function.
this is the old code
private string user;
public string User
{
get { return user; }
set
{
user = value;
Notify("user");
}
}
I don't know how to manage user = value;
This is my try:
private List<string> user;
public List<string> User
{
get { return user; }
set
{
user.Add(value);
Notify("user");
}
}
It throws an error because value is a List and can't be added to a list.
edit: sorry, it was a copy and paste mistake. I changed the old code with this edit.
So, what I want to do is binding a List to a column of a datagrid. The code above is part of an data class.
This is the code in the window.xaml
<DataGridTextColumn Header = "user" Binding="{Binding Path=User, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" MinWidth="50" />
In your example value would be of the same type as the Property, List<string>. So you can't use user.Add(value). Rather, you can write:
private List<string> user = new List<string>();
public List<string> User
{
get { return user; }
set
{
user = value;
Notify("user");
}
}
But if you are using Binding Users to a DataGrid, then it is better to use a collection that implements INotifyPropertyChanged like ObservableCollection<T>. That way, the view (DataGrid) is notified when an item is added/removed from the collection or the whole collection is refreshed.
private ObservableCollection<string> user = new ObservableCollection<string>();
public ObservableCollection<string> User
{
get { return user; }
set
{
user = value;
Notify("user");
}
}
For more information about binding an ObservableCollection to a DataGrid, check out this article.
And by the way, I think the property is better named Users not User because it's a list of users.

Best way to store user's input in a multi-step application [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
My program consists of a number of steps. Say each step is in a tabpage on which upon completion, the user selects "Next".
What is the best/fastest way of storing these information?
I would generally say that this would be one Model to keep all the data - which then gets forwarded to the processing method once the user says "ok" at the last page. BUt then this depends a lot on actual usage scenarios, so that may not be the best way all the time.
I created a separate class that had properties for the 'answers' on the individual pages. Not sure if this is the best way to do it, but it worked perfectly for me. The user could go back and forth between the pages and the answers were stored. To Prescott's point, if they need to come back later perhaps you could save their partial form out as XML.
Create a viewmodel that represents the wizard, and in that viewmodel all the data related to your steps. You can also keep track of current step, and implement some methods to enable/disable next and previous buttons etc.
Your actual wizard window and tabs (the views) then all work with the wizard viewmodel (the data).
I would go with In-Memory Repository if you just want to retain this information for program execution, For example
namespace Repository {
public class InMemoryRepository : IRepository{
static readonly List<Item> items = new List<Item> {
new Item("CurrentTaxRate", 20m),
new Item("CurrentAmount", 100m)
};
public Item Get(string name) {
return items.Single(x => x.Name == name);
}
}
}
public class Item {
public Item(string name, decimal value) {
Name = name;
Value = value;
}
public string Name { get; private set; }
public decimal Value { get; private set; }
}
You can bolt on Add, Delete or Edit functionality easily to this and replace the "Item" here with your own "UserData" type.
Use dependency injection for getting this information back
public class TaxRate : ITaxRate {
private readonly IRepository repository;
public TaxRate(IRepository repository) {
this.repository = repository;
}
public decimal Get() {
var item = repository.Get("CurrentTaxRate");
return item.Value;
}
}

Use a List and Variables in different Pages in WPF with C# [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a list like this on my home page on my WPF (C#) project like this:
var sintomas = new List<string>();
I need to add data to that list through different pages from some Radio Buttons so I can set for example in my second page:
if(RadioButton.isChecked)
sintomas.Add("Something");
What do I have to do to initialize it and then use it on each page?
Same for variables.
I think you need to do
Public static List<string>() sintomas = new List<string>();
Then in your other class:
private YourClass Yourname = new YourClass();
Then you could acces the list like this:
Yourname.sintomas.add("yay");
Update
On the start of your class so after:
public partial class AddisHome : Window
{
You need to create your list like this:
Public List<string> sintomas = new List<string>();
It's public so other classes can acces it
Then on your second class:
again on the top of your other class after:
public partial class Personaldata : Window
{
You need to add this:
private AddisHome yourvar = new AddisHome();
then inside your function you can call:
yourvar.sintomas.add("yay");

Display datagrid based on one filter in wpf [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have datagrid and one value slider.I have three columns .Three columns like name,age and telephone number.Filter value is set to age.If i change slider value that is filter(age) based on filter DataGrid needs to display the data.I am using observable collection.
I think i understood what you want exactly.
Assuming that the name of your Data Grid is "MyDataGrid" and it's data source is bound to a class "MyDataGridItem" with this structure
public class MyDataGridItem
{
public string Name { get; set; }
public int Age { get; set; }
}
Subscribe the ValueChanged event of the slider and get the value and use LINQ where query to filter your results.
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var sliderValue = (int) MySlider.Value;
MyDataGrid.ItemsSource = students.Where(item =>item.Age<sliderValue);
}

get model from List of a model [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there a way to get the specific items from the list of a model without using a foreach of the list? like if you return a List<model> holding a model with a few differnt items to specifically call those item.
If you want a list of a specific item that's within your list then you can use LINQ to return those items, for example:
var customerNames = customersList.Select(customer => customer.Name);
This would give you a string collection with all of your customer names equal to the amount that was in your original list.
Or if you would like to retrieve a number of different items from your model list then you may want to look into creating a class specifically for stripping out those items, for example:
public class CustomerContactDetails
{
public string Address { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
}
...
var contactDetails = customersList.Select(customer => new CustomerContactDetails { Address = customer.Address, Email = customer.Email, Telephone = customer.Telephone });
You can use C# LambdaExpression, like this:
var yourModel = modelList.Where(x => x.Id = 12).Select(x).First();

Categories

Resources