简体   繁体   English

动态地将行添加到DataView的索引

[英]Dynamically adding rows to an index of a DataView

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. Category = 0,SubCategory1 = 1,SubCategory2 =2。这将返回一个不错的DataView,其中包含所有正确的信息。

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. 但是,当我用当前代码插入时,我觉得以某种方式插入会抛出DataView索引。 Here is what I have so far. 这是我到目前为止所拥有的。 Please forgive me if I am completely missing something. 如果我完全缺少某些东西,请原谅我。 I am new to DataViews. 我是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. 因为当我检查比插入位置更高的索引时,插入的数据似乎重复出现或未在正确的索引处插入。

It might be easier to use a ObservableCollection and a CollectionViewSource. 使用ObservableCollection和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. 使用ObservableCollection.Add(item),然后刷新CollectionViewSource,如果您正确地设置了排序和分组设置,它将自动对数据进行排序和分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM