How to: Bind a DataView Object to a Windows Forms DataGridView Control

The DataGridView control provides a powerful and flexible way to display data in a tabular format. The DataGridView control supports the standard Windows Forms data binding model, so it will bind to DataView and a variety of other data sources. In most situations, however, you will bind to a BindingSource component that will manage the details of interacting with the data source.

For more information about the DataGridView control, see DataGridView Control Overview (Windows Forms).

To connect a DataGridView control to a DataView

  1. Implement a method to handle the details of retrieving data from a database. The following code example implements a GetData method that initializes a SqlDataAdapter component and uses it to fill a DataSet. Be sure to set the connectionString variable to a value that is appropriate for your database. You will need access to a server with the AdventureWorks SQL Server sample database installed.

    
    private void GetData()
    {
        try
        {
            // Initialize the DataSet.
            dataSet = new DataSet();
            dataSet.Locale = CultureInfo.InvariantCulture;
    
            // Create the connection string for the AdventureWorks sample database.
            string connectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;"
                + "Integrated Security=true;";
    
            // Create the command strings for querying the Contact table.
            string contactSelectCommand = "SELECT ContactID, Title, FirstName, LastName, EmailAddress, Phone FROM Person.Contact";
    
            // Create the contacts data adapter.
            contactsDataAdapter = new SqlDataAdapter(
                contactSelectCommand,
                connectionString);
    
            // Create a command builder to generate SQL update, insert, and
            // delete commands based on the contacts select command. These are used to
            // update the database.
            SqlCommandBuilder contactsCommandBuilder = new SqlCommandBuilder(contactsDataAdapter);
    
            // Fill the data set with the contact information.
            contactsDataAdapter.Fill(dataSet, "Contact");
    
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    
    
  2. In the Load event handler of your form, bind the DataGridView control to the BindingSource component and call the GetData method to retrieve the data from the database. The DataView is created from a LINQ to DataSet query over the Contact DataTable and is then bound to the BindingSource component.

    
    private void Form1_Load(object sender, EventArgs e)
    {
        // Connect to the database and fill the DataSet.
        GetData();
    
        contactDataGridView.DataSource = contactBindingSource;
    
        // Create a LinqDataView from a LINQ to DataSet query and bind it 
        // to the Windows forms control.
        EnumerableRowCollection<DataRow> contactQuery = from row in dataSet.Tables["Contact"].AsEnumerable()
                                                        where row.Field<string>("EmailAddress") != null
                                                        orderby row.Field<string>("LastName")
                                                        select row;
    
        contactView = contactQuery.AsDataView();
    
        // Bind the DataGridView to the BindingSource.
        contactBindingSource.DataSource = contactView;
        contactDataGridView.AutoResizeColumns();
    }
    
    
    
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft