Using a subset of columns in a datatable as a DataSource for Windows controls.

A short one today.

Say that you have a DataTable that is created dynamically or is, for example, passed to you from a webservice method.

This table may contain several columns that you have no interest of displaying to your end user.

So you get your DataTable, you have your DataGridView control and you simply set this table as the grids datasource:

yourDataGrid.DataSource = yourDataTable;

This will display all columns and all rows.

However, you may only want to display some columns and some rows. So how do you do that?

If you have control of how the DataTable is created or if you use the wizards to create the source for the grid etc, this is not that hard.

But when you are just passed a DataTable, one way to do this is as follows.

In this example, we receive a DataTable holding some customer information.

However, we are only interest in showing customers whose first name starts with “A” and we only want to show the customers first and last name and address.

Just create a Win Form application, drop a button and datagridview onto the form. Then add the following code:

        private void button1_Click(object sender, EventArgs e)

        {

            DataTable dt = FakeWebService();

            // Binding the table now will display all rows and all columns:

            //dataGridView1.DataSource = dt;

            // Create a view from the table

            DataView displayView = new DataView(dt);

            // Filter rows so that we only have rows starting with A

            displayView.RowFilter = "CustFName like 'A%'";

            // Binding the view now will display all rows with the filter, but also all columns:

            //dataGridView1.DataSource = displayView;

            // What we want to do now is to create a new table that only contains the columns

      // we are interested in; CustFName, CustLName, CustAddress

            DataTable tmpTable = displayView.ToTable(false, "CustFName", "CustLName", "CustAddress");

            dataGridView1.DataSource = tmpTable;

            // or the shorter version:

            //dataGridView1.DataSource = displayView.ToTable(false, "CustFName", "CustLName", "CustAddress");

        }

        private DataTable FakeWebService()

        {

            DataTable dt = new DataTable("CustTable");

            // Add some columns and some rows

            dt.Columns.Add(new DataColumn("CustId", typeof(string)));

            dt.Columns.Add(new DataColumn("CustFName", typeof(string)));

            dt.Columns.Add(new DataColumn("CustLName", typeof(string)));

            dt.Columns.Add(new DataColumn("CustAddress", typeof(string)));

            dt.Columns.Add(new DataColumn("CustPhone", typeof(string)));

            dt.Rows.Add(new object[] { "ABSMI", "Abel", "Smith", "Anwhere 1", "123456" });

            dt.Rows.Add(new object[] { "PERIC", "Pete", "Rich", "Overthere 8", "555466" });

            dt.Rows.Add(new object[] { "CAPED", "Carl", "Pedal", "Thatway 2", "654321" });

            dt.Rows.Add(new object[] { "MISPI", "Mike", "Spike", "Noway 4", "456789" });

            dt.Rows.Add(new object[] { "ANMAI", "Anna", "Maine", "Myway 1", "987654" });

            dt.Rows.Add(new object[] { "THTHO", "Thom", "Thomson", "Somewhere 6", "666555" });

            return dt;

        }

This will now populate the grid with rows where the first name starts with “A” and it will only display the specified columns.

"DataViews (ADO.NET)"

https://msdn.microsoft.com/en-us/library/fdcwwhez.aspx

"DataView Class"

https://msdn.microsoft.com/en-us/library/system.data.dataview.aspx

"Data Sources Supported by Windows Forms"

https://msdn.microsoft.com/en-us/library/f3y6cb0c.aspx