Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Das DataGridView-Steuerelement ermöglicht die flexible Anzeige von Daten in tabellarischer Form. Das DataGridView unterstützt das Standard-Datenbindungsmodell von Windows Forms und ermöglicht so die Bindung an eine DataView und eine Vielzahl anderer Datenquellen. In den meisten Fällen dürfte jedoch eine Bindung an eine BindingSource-Komponente erfolgen, die sich um die Details der Interaktion mit der Datenquelle kümmert.
Weitere Informationen zum DataGridView Steuerelement finden Sie unter DataGridView Control Overview.
So verbinden Sie ein "DataGridView"-Steuerelement mit einer "DataView"
Implementieren Sie eine Methode, mit der die Details des Abrufens von Daten aus einer Datenbank behandelt werden. Das folgende Codebeispiel implementiert eine
GetData
-Methode, die eine SqlDataAdapter-Komponente initialisiert, und verwendet diese, um ein DataSet aufzufüllen. Sorgen Sie dafür, dass dieconnectionString
-Variable auf einen Wert gesetzt wird, der für Ihre Datenbank geeignet ist. Sie benötigen Zugriff auf einen Server, auf dem die SQL Server-AdventureWorks-Beispieldatenbank installiert ist.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
Binden Sie im Load-Ereignishandler Ihres Formulars das DataGridView-Steuerelement an die BindingSource-Komponente, und verwenden Sie die
GetData
-Methode, um Daten aus der Datenbank abzurufen. Die DataView wird über die LINQ to DataSet-Abfrage über den Kontakt DataTable erstellt und dann an die BindingSource-Komponente gebunden.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