动态地将行添加到 DataView 的索引中
我在将数据行插入数据视图时遇到一个有趣的问题。我从数据库收到一组数据。此信息已分组。该数据集分为三个级别。例如:
Category SaleValue
SubCategory1 SaleValue
SubCategory2 SaleValue
SubCategory2 SaleValue
SubCategory1 SaleValue
SubCategory1 SaleValue
Category SaleValue
...
我为分组分配了整数值。 Category = 0,SubCategory1 = 1,SubCategory2 = 2。这将返回一个包含所有正确信息的漂亮 DataView。
我的问题在于如何在特定索引处插入新数据。该报告还有一层数据。我检索到最后一层数据。这由每个级别的产品组成。例如(基于上面的示例构建)。
Category SaleValue
SubCategory1 SaleValue
SubCategory2 SaleValue
Product SaleValue
Product SaleValue
SubCategory2 SaleValue
Product SaleValue
SubCategory1 SaleValue
SubCategory1 SaleValue
Category SaleValue
...
我需要将这些数据加入到适当的部分中。但是,我觉得当我使用当前代码进行插入时,插入会导致 DataView 索引远远偏离。这是我到目前为止所拥有的。如果我完全遗漏了什么,请原谅我。我是 DataView 新手。
private DataView AddProducts(DataView data)
{
int position = 0;
for (int i = position; i < data.Count; i++)
{
DataRowView currentRow = data[i];
if (current.Row.Field<int>("group") == 2)
{
var productData = //DB call for data
foreach (DataRowView row in productData)
{
position = i+1; //Dont want to insert at the row, but after.
DataRow newRow = data.Table.NewRow();
newRow[0] = row["ProductName"];
newRow[1] = row["Sale"];
data.Table.Rows.InsertAt(newRow, position);
// i is now position. This will allow another insert to insert on the
// next row, or for the loop to start at the row after this inserted
// row.
i = position;
}
}
}
return data;
}
这似乎是我错过了一些东西。也许是因为我将循环的上限设置为原始 data.Count 数字?插入是否弄乱了索引?因为当我检查比插入位置更高的索引时,插入的数据似乎重复或未插入到正确的索引处。
I have an interesting issue with inserting rows of data into a dataview. I receive a set of data from the database. This information is grouped. There are three levels from this data set. For example:
Category SaleValue
SubCategory1 SaleValue
SubCategory2 SaleValue
SubCategory2 SaleValue
SubCategory1 SaleValue
SubCategory1 SaleValue
Category SaleValue
...
I have assigned integer values for the grouping. Category = 0, SubCategory1 = 1, SubCategory2 = 2. This returns a nice DataView with all of the correct information.
My problem lies in how to insert new data at specific indexes. There is one more level of data for the report. There is one final level of data that I retrieve. This consists of products for each level. For example (building off the above example).
Category SaleValue
SubCategory1 SaleValue
SubCategory2 SaleValue
Product SaleValue
Product SaleValue
SubCategory2 SaleValue
Product SaleValue
SubCategory1 SaleValue
SubCategory1 SaleValue
Category SaleValue
...
I need to join in this data into the appropriate sections. However, I feel that somehow when I am inserting with my current code, the inserts are throwing the DataView indexes way off. Here is what I have so far. Please forgive me if I am completely missing something. I am new to DataViews.
private DataView AddProducts(DataView data)
{
int position = 0;
for (int i = position; i < data.Count; i++)
{
DataRowView currentRow = data[i];
if (current.Row.Field<int>("group") == 2)
{
var productData = //DB call for data
foreach (DataRowView row in productData)
{
position = i+1; //Dont want to insert at the row, but after.
DataRow newRow = data.Table.NewRow();
newRow[0] = row["ProductName"];
newRow[1] = row["Sale"];
data.Table.Rows.InsertAt(newRow, position);
// i is now position. This will allow another insert to insert on the
// next row, or for the loop to start at the row after this inserted
// row.
i = position;
}
}
}
return data;
}
This just seems like I am missing something. Maybe because I am setting the upper bounds of the loop to be the original data.Count number? Is the insert messing up the indexes? Because when I check a higher index than where I inserted, the inserted data seems to be repeated or not inserted at the right indexes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 ObservableCollection 和 CollectionViewSource 可能会更容易。
使用 ObservableCollection.Add(item),然后刷新 CollectionViewSource,如果您的排序和分组设置正确,它将自动对数据进行排序和分组。
It might be easier to use a ObservableCollection and a CollectionViewSource.
Use ObservableCollection.Add(item) and then refresh the CollectionViewSource which will automatically sort and group the data if you have the sorting and grouping setup correctly.