Share via


How to: Change Displayed Data at Run Time in the Windows Forms DataGrid Control

Note

The DataGridView control replaces and adds functionality to the DataGrid control; however, the DataGrid control is retained for both backward compatibility and future use, if you choose. For more information, see Differences Between the Windows Forms DataGridView and DataGrid Controls.

After you have created a Windows Forms DataGrid using the design-time features, you may also wish to dynamically change elements of the DataSet object of the grid at run time. This can include changes to either individual values of the table or changing which data source is bound to the DataGrid control. Changes to individual values are done through the DataSet object, not the DataGrid control.

To change data programmatically

  • Specify the desired table from the DataSet object and the desired row and field from the table and set the cell equal to the new value.

    Note

    To specify the first table of the DataSet or the first row of the table, use 0.

    The following example shows how to change the second entry of the first row of the first table of a dataset by clicking Button1. The DataSet (ds) and Tables (0 and 1) were previously created.

    Protected Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ds.tables(0).rows(0)(1) = "NewEntry"
    End Sub
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       ds.Tables[0].Rows[0][1]="NewEntry";
    }
    
    private: 
       void button1_Click(System::Object^ sender, System::EventArgs^ e)
       {
          dataSet1->Tables[0]->Rows[0][1] = "NewEntry";
       }
    

    (Visual C#, Visual C++) Place the following code in the form's constructor to register the event handler.

    this.button1.Click += new System.EventHandler(this.button1_Click);
    
    this->button1->Click +=
       gcnew System::EventHandler(this, &Form1::button1_Click);
    

    At run time you can use the SetDataBinding method to bind the DataGrid control to a different data source. For example, you may have several ADO.NET data controls, each connected to a different database.

To change the DataSource programmatically

  • Set the SetDataBinding method to the name of the data source and table you want to bind to.

    The following example shows how to change the date source using the SetDataBinding method to an ADO.NET data control (adoPubsAuthors) that is connected to the Authors table in the Pubs database.

    Private Sub ResetSource()
       DataGrid1.SetDataBinding(adoPubsAuthors, "Authors")
    End Sub
    
    private void ResetSource()
    {
       DataGrid1.SetDataBinding(adoPubsAuthors, "Authors");
    }
    
    private:
       void ResetSource()
       {
          dataGrid1->SetDataBinding(adoPubsAuthors, "Authors");
       }
    

See Also

Tasks

How to: Delete or Hide Columns in the Windows Forms DataGrid Control

How to: Add Tables and Columns to the Windows Forms DataGrid Control

How to: Bind the Windows Forms DataGrid Control to a Data Source

Concepts

ADO.NET DataSets