Delen via


Procedure: Een DataView-object binden aan een Windows Forms DataGridView-besturingselement

Het DataGridView besturingselement biedt een krachtige en flexibele manier om gegevens in tabelvorm weer te geven. Het DataGridView besturingselement ondersteunt het standaardgegevensbindingsmodel van Windows Forms, zodat het wordt verbonden met DataView en een verscheidenheid aan andere gegevensbronnen. In de meeste situaties verbindt u echter met een BindingSource onderdeel waarmee de details van interactie met de gegevensbron worden beheerd.

Zie DataGridView Control Overview voor meer informatie over het DataGridView besturingselement.

Een DataGridView-besturingselement verbinden met een DataView

  1. Implementeer een methode voor het afhandelen van de details van het ophalen van gegevens uit een database. In het volgende codevoorbeeld wordt een GetData methode geïmplementeerd waarmee een SqlDataAdapter onderdeel wordt geïnitialiseerd en gebruikt om een DataSetcomponent in te vullen. Zorg ervoor dat u de connectionString variabele instelt op een waarde die geschikt is voor uw database. U hebt toegang nodig tot een server waarop de voorbeelddatabase AdventureWorks SQL Server is geïnstalleerd.

    void GetData()
    {
        try
        {
            // Initialize the DataSet.
            _dataSet = new DataSet
            {
                Locale = CultureInfo.InvariantCulture
            };
    
            // Create the connection string for the AdventureWorks sample database.
            const string connectionString = "...";
    
            // Create the command strings for querying the Contact table.
            const 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.
            var contactsCommandBuilder = new SqlCommandBuilder(_contactsDataAdapter);
    
            // Fill the data set with the contact information.
            _contactsDataAdapter.Fill(_dataSet, "Contact");
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    Private Sub GetData()
        Try
            ' Initialize the DataSet.
            dataSet = New DataSet()
            dataSet.Locale = CultureInfo.InvariantCulture
    
            ' Create the connection string for the AdventureWorks sample database.
            Dim connectionString As String = "..."
    
            ' Create the command strings for querying the Contact table.
            Dim contactSelectCommand As String = "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.
            Dim contactsCommandBuilder As SqlCommandBuilder = New SqlCommandBuilder(contactsDataAdapter)
    
            ' Fill the data set with the contact information.
            contactsDataAdapter.Fill(dataSet, "Contact")
    
        Catch ex As SqlException
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    
  2. Bind in de Load gebeurtenis-handler van uw formulier het besturingselement aan het DataGridView BindingSource onderdeel en roep de GetData methode aan om de gegevens op te halen uit de database. De DataView query wordt gemaakt van een LINQ naar DataSet-query via de contactpersoon DataTable en is vervolgens gebonden aan het BindingSource onderdeel.

    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();
    }
    
    Private Sub Form1_Load(ByVal sender As System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles MyBase.Load
        ' 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.
        Dim contactQuery = _
            From row In dataSet.Tables("Contact").AsEnumerable() _
            Where row.Field(Of String)("EmailAddress") <> Nothing _
            Order By row.Field(Of String)("LastName") _
            Select row
    
        contactView = contactQuery.AsDataView()
    
        ' Bind the DataGridView to the BindingSource.
        contactBindingSource.DataSource = contactView
        contactDataGridView.AutoResizeColumns()
    End Sub
    

Zie ook