简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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