Anvisningar: Binda ett DataView-objekt till en Windows Forms DataGridView-kontroll
Kontrollen DataGridView ger ett kraftfullt och flexibelt sätt att visa data i tabellformat. Kontrollen DataGridView stöder standardmodellen för Windows Forms-databindning, så den binder till DataView och en mängd andra datakällor. I de flesta fall binder du dock till en BindingSource komponent som hanterar information om hur du interagerar med datakällan.
Mer information om kontrollen finns i DataGridView Översikt över DataGridView-kontroll.
Ansluta en DataGridView-kontroll till en DataView
Implementera en metod för att hantera information om hur du hämtar data från en databas. I följande kodexempel implementeras en
GetData
metod som initierar en SqlDataAdapter komponent och använder den för att fylla i en DataSet. Se till att ange variabelnconnectionString
till ett värde som är lämpligt för databasen. Du behöver åtkomst till en server med AdventureWorks SQL Server-exempeldatabasen installerad.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
Load I formulärets händelsehanterare binder DataGridView du kontrollen till komponenten BindingSource och anropar
GetData
metoden för att hämta data från databasen. DataView Skapas från en LINQ till DataSet-fråga över kontakten DataTable och binds sedan till komponentenBindingSource.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