Reader Level:

ComboBox - DataGridView Sample

By Mayur Dighe on Aug 22, 2011
The Article Demonstrate the how to change the DataSource Property of DataGridView Dynamically.

The Article Demonstrate the how to change the DataSource Property of DataGridView Dynamically.


  • ComboBox Control
  • DataGridView Control


  • The method is so easy to handle.
  • Here, I am taken an XML file as DataBase file.
  • The Article shows displaying data into DataGridView Control of table name selected from the ComboBox Control.


  1. Create a new instance of the System.Data.DataSet class.

    DataSet Dset = new DataSet();

    Listing 1
  2. Now, on the Form_Load event handler read an XML file using DataSet Object.


    Listing 2
  3. Then add all the TableName avail in the DataSet Object to the ComboBox items collection. Result is displayed in Figure 1.

    for (int item=0; item < Dset.Tables.Count;item++ )

    Listing 3


    Figure 1

  4. After this, set the DataSource property of DataGridView to the selected item of ComboBox.

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
             dataGridView1.DataSource = null;
             dataGridView1.DataSource = Dset.Tables[comboBox1.Text];

    Listing 4

Output: Following snapshots shows the intended result.

ComboBox - DataGridView Sample

Figure 2

ComboBox - DataGridView Sample

Figure 3


In this session, we have seen the ComboBox - DataGridView operation for displaying table data dynamically from the selected item of ComboBox Control.

Mayur Dighe
Mayur Dighe

Mayur B. Dighe – is a Hobbyist Programmer and Engineering Student. He completed a Diploma in Computer Technology in 2011 and also currently pursuing IT Engineering from University of Pune, India.My Blog : &... Read more



Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.

Trending up