HOW TO:將 DataView 物件繫結至 Windows Form DataGridView 控制項
DataGridView 控制項會提供一套功能強大且彈性的方式,讓您以表格式格式顯示資料。 DataGridView 控制項支援標準的 Windows Form 資料繫結模型,因此它將繫結至 DataView 和各種其他資料來源。 不過,在大部分情況下,您會繫結至 BindingSource 元件,以便管理與資料來源互動的詳細資料。
如需 DataGridView 控制項的詳細資訊,請參閱 DataGridView 控制項概觀 (Windows Form)。
若要將 DataGridView 控制項連接至 DataView
實作一種方法來處理從資料庫中擷取資料的詳細資料。 下列程式碼範例會實作 GetData 方法,而此方法會初始化 SqlDataAdapter 元件並用它來填入 DataSet。 請務必將 connectionString 變數設定為適用於您資料庫的值。 您將需要已安裝 AdventureWorks SQL Server 範例資料庫之伺服器的存取權。
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); } }
在表單的 Load 事件處理常式中,將 DataGridView 控制項繫結至 BindingSource 元件並呼叫 GetData 方法,以便從資料庫中擷取資料。 DataView 是根據針對 Contact DataTable 的 LINQ to DataSet 查詢所建立,然後它會繫結至 BindingSource 元件。
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(); }