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 Übersicht über das DataGridView-Steuerelement (Windows Forms).
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 die connectionString-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.
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 = "Data Source=localhost;Initial Catalog=AdventureWorks;" _ & "Integrated Security=true;" ' 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
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); } }
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 Contact-DataTable aus einer LINQ to DataSet-Abfrage erstellt und dann an die BindingSource-Komponente gebunden.
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
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(); }