简体   繁体   中英

Spreading the contents of a DataTable into 3 DataGridViews

Let's say that I have a form which runs a stored procedure. This stored procedure creates some rows in a table with pre-generated values and returns me a DataTable containing the rows created by this stored procedure.

On the form, I need to display this information on 3 different DataGridViews, so the user can change it. The schema is the same but each of the DataGridViews will display different categories and hence hide some irrelevant columns in each of the DataGridView, but in the database, they are all part of the same table. New rows may be added by the user on all 3 DataGridViews.

I am a bit confused how to display the information from a single DataTable into three different DataGridViews and still have an easy way to update the database with the changes made by the user to the DataGridViews.

I assume that I could break my main DataTable in three of them and then bind each DataTable to the relevant DataGridView, but wouldn't it pose problems when I will want to save the changes (updated and new rows) to the database considering that my changes are spread into 3 DataTables instead of a single one?

Would there be a better way to achieve this rather than splitting the main DataTable in the first place?

Thanks a lot in advance.

All the DataGridViews need their own DataView. The easiest way may be to use separate BindingSource components.

When you state:

 dataGridView1.DataSource = dataTable1;

You are actually using the default DataView of the Table. You are looking for something like:

//untested
var view1 = new DataView(dataTable1);
dataGridView1.DataSource = view1;
var view2 = new DataView(dataTable1);
dataGridView2.DataSource = view2;

And then you can use view1, view2 to control filtering and sorting.

Thanks a lot Henk, your post led me on the right track and it solved my problem perfectly. I can now add items on any grid views and my DataTable is updated without needing to do anything like merging like I was expecting I may have to do.

In order to try and understand the solution, I've made a little test demo, which I thought I would post here for future readers, as it includes how to filter each DataView to only include the revelant information. It is an example code, I didn't include error checkings, etc.

private DataTable fruitsDataTable = null;
private DataView orangesDataView = null;
private DataView applesDataView = null;

private void Form1_Load(object sender, EventArgs e)
    {
        fruitsDataTable = new DataTable("Fruits");

        // Dynamically create the DataTable schema for the sake of this example
        fruitsDataTable.Columns.Add("Category", typeof(string));
        fruitsDataTable.Columns.Add("Description", typeof (string));
        fruitsDataTable.Columns.Add("Quantity", typeof(int));
        fruitsDataTable.Columns.Add("Price", typeof(double));

        // Add the fruits to the main table
        fruitsDataTable.Rows.Add("ORANGE", "Fresh Oranges", 5, 5.50);

        fruitsDataTable.Rows.Add("APPLE", "Granny Smith Apples", 10, 2.20);
        fruitsDataTable.Rows.Add("APPLE", "Golden Apples", 40, 1.75);

        fruitsDataTable.Rows.Add("ORANGE", "Bloody Oranges", 10, 7.99);

        fruitsDataTable.Rows.Add("BANANA", "Ivory Coast Bananas", 5, 6.99);

        mainGridView.DataSource = fruitsDataTable;

        // Create a DataView for each fruit category and bind it to the relevant DataGridView control on the form
        orangesDataView = new DataView(fruitsDataTable, "Category = 'ORANGE'", string.Empty, DataViewRowState.CurrentRows);
        orangesGridView.DataSource = orangesDataView;

        applesDataView = new DataView(fruitsDataTable, "Category = 'APPLE'", string.Empty, DataViewRowState.CurrentRows);
        applesGridView.DataSource = applesDataView;
    }

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