Firstly I'd like to point out that you're not really using GridView
the way it is meant to be used. GridView
is for showing a list of data. You shouldn't be extracting data out of it by enumerating rows in the UI. The UI displays data from your data source. You edit the data source and the UI updates. If changes are made in the UI then it automatically updates the data source and you just go from there. The only possible case I can see where you'd use the GridView
like you are is if you displayed a grid of items and all fields are editable but that isn't really what GridView
is designed for either. Nevertheless let's fix the problem you have with your existing code.
You said you have this
1 20 7 30
2 10 7 30
2 10 8 20
3 10 8 20
But want this. Note that it is unclear how these numeric values actually map to your ItemAllocation
type that you're creating.
1 20 7 30
2 10 7 30
3 20 8 20
You didn't tell us the rules you are following to get there. I can see you're grouping by the Box
but after that it is unclear. The first and last rows are just from your original data set so we can infer nothing here. But box 2 has two rows and it seems like you just selected the first one. To get this aggregated data you'll need to use LINQ to fetch the data you want for the second UI. Perhaps something like this.
//Represents the data you said you had but completely guessing at values
var gridSrc = new[]
{
new ItemAllocation() { BoxNo = 1, Name = "20", EachQty = 7, Article = "30" },
new ItemAllocation() { BoxNo = 2, Name = "10", EachQty = 7, Article = "30" },
new ItemAllocation() { BoxNo = 2, Name = "10", EachQty = 8, Article = "20" },
new ItemAllocation() { BoxNo = 3, Name = "10", EachQty = 8, Article = "20" },
};
//You didn't specify the rules so assuming group by BoxNo and then grab remaining values from first one
var groupedItems = from i in gridSrc
group i by i.BoxNo into g
select new ItemAllocation()
{
BoxNo = g.Key,
Name = g.First().Name,
EachQty = g.First().EachQty,
Article = g.First().Article
};
var finalItems = groupedItems.ToArray();
This is using grouping but there are other approaches as well including using Distinct and a custom comparer or storing the values into a dictionary by BoxNo and selecting just the first item.