Hello,
Now i want to get a list of data from the existed rows in the xamgrid's column, is there any API to get it?
For example, there are 5 rows in the xamgrid
Country
1 UK
2 USA
3 USA
4 UK
5 UK
I want to get a list List<string> countries. How could I get it?
Since the grid is bound, the most efficient route would most likely be to get the data off your data model itself rather then through the grid.
Something like this
List<Person> list = new List<Person>();
list.Add(new Person() { StateCode = "NJ" });
list.Add(new Person() { StateCode = "NY" });
list.Add(new Person() { StateCode = "PA" });
IEnumerable<string> stateCodes =list.Select( a => a.StateCode );
stateCodes =stateCodes.Distinct().ToList();
This way you skip using the grid and the overhead associated with navigating the entire collection via the grid.
Thank you very much for the response.
But what I want is I only want the list of the values which are shown in the grid. I will use this list for my filter combobox. In your answer, I might have 5 values in the list (data source), but only 2 of them are shown as rows, then I can only get 5 values list with your code but not 2.
I have found the solution, and it is shown as following:
List<string> stateCodes=new List<String>
foreach (var item in XamGrid.ItemsSource)
{
Person person = item as Person;
stateCodes.Add(person.StateCode);
}
return stateCodes.Distinct().ToList();
Thanks anyway
The code I provided assumes that the List<Person> is your grid's ItemsSource. The code you wrote is doing the exact same thing.
Yes, I was not able to cast ItemSource to List, so I used those code. Now I found there is Cast() function can be used.
Thank you very much!!