Procedura: associare un oggetto DataView a un controllo DataGridView Windows Form
Il controllo DataGridView fornisce un sistema efficiente e flessibile per visualizzare i dati in formato tabulare. Il controllo DataGridView supporta il modello di data binding standard di Windows Form ed è quindi possibile associarlo a DataView e a varie altre origini dati. Nella maggior parte dei casi, tuttavia, l'associazione viene eseguita a un componente BindingSource, che gestisce i dettagli dell'interazione con l'origine dati.
Per altre informazioni sul controllo DataGridView, vedere Panoramica del controllo DataGridView.
Per connettere un controllo DataGridView a un oggetto DataView
Implementare un metodo per gestire i dettagli del recupero di dati da un database. Nell'esempio di codice seguente viene implementato un metodo
GetData
che inizializza un componente SqlDataAdapter e lo usa per compilare DataSet. Assicurarsi di impostare la variabileconnectionString
su un valore appropriato per il database. Sarà necessario disporre di accesso a un server in cui sia installato il database SQL Server di esempio AdventureWorks.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
Nel gestore eventi Load del form associare il controllo DataGridView al componente BindingSource e chiamare il metodo
GetData
per recuperare i dati dal database. L'oggetto DataView viene creato da una query LINQ to DataSet sul contatto DataTable e viene quindi associato al componente BindingSource.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