![]() |
Languages »
VB.NET »
General
Intermediate
License: The Code Project Open License (CPOL)
DataGridView with Detail Edit Form - VS 2005By Robert R FreemanHow to create a DataGridView with an associated Detail Edit Form using a strongly typed dataset data layer. |
VB 8.0, Windows, .NET 2.0, .NET 3.0, ADO.NET, WinForms, VS2005, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

This article is a tutorial on how to create a WinForms selectable datagridview with a separate Form for editing detail row data using a strongly typed DataSet data layer. We will use Visual Studio 2005 and .NET 2.0.
Creating an editable datagrid is a very straightforward way to edit a datagrid's values, but sometimes a separate editable Form/UserControl is desired (increased flexibility, more detail, etc.). This example will load the editable fields and lookups with the grid on Form load. This will keep us from having to synchronize and manage separate instances of similar data and will reduce round trips to the database. When dealing with lots of records, you would want to implement a where filter in your query. We will include a Save button on the Edit form. The Save button could just as easily be implemented on the main form. The edit form could also be changed to a UserControl to display on the main form instead of a popup.
Orders and Order. Orders in Design View. Add New Datasource in the Data Sources Window.
Database as the Datasource Type. Microsoft SQL Server (SqlClient) as the Data Source. OrderID, CustomerID, and EmployeeID from the Orders table, CustomerID, CompanyName, and ContactName from the Customers table, and EmployeeID, LastName,FirstName and Title from the Employees table as below:



This automatically creates the NorthwindDataSet strongly typed dataset with related tables/tableAdapters.

CustomerID and EmployeeID columns to ComboBoxes in the Data Sources Window.

Orders table from the Data Sources Window onto the Orders form.

NorthWindDataSet instance, a BindingSource, and a BindingNavigator. BindingNavigator.

DataGridView's "Smart Tag" to open the DataGridView Tasks Window.
![]()
Edit Columns...Change the CustomerID and EmployeeID columns to DataGridViewComboBoxColumn ColumnTypes.
Datasource, DisplayMember, and ValueMember properties for the Customer and Employee columns.
Selecting the DataSources from the Project Data Sources node automatically creates the BindingSources and TableAdapters for the Form.
DisplayStyle to Nothing for both columns since these will be read-only. Order form in design view. NorthwindDataSet.Orders table in the DataSources Window from DataGridView to Details.

DataSources Window onto the Order form. This will create the 3 controls on the form as well as the DataSet, BindingSource, TableAdapter, and BindingNavigator. BindingNavigator. Button control on the form and rename it ButtonSave. ComboBoxes' "Smart Tag" to edit the DataSource, DisplayMember, SelectedValue, and ValueMember properties as we did earlier with the GridViewComboBoxColumns. Use data bound items option to edit these values.
![]()

BindingSource and TableAdapter components on the form for the Employees and Customers tables. TableAdapters (Not the OrdersTableAdapter) since we will be filling these tables from the Orders form. DataBindings.Text property to None since we're now binding to the SelectedValue property.

New() in the Edit Form.

Save button in design mode to create a click event handler. Private Sub Orders_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
'Reject any changes that were not saved.
Me.NorthwindDataSet.Orders(Me.OrdersBindingSource.Position)._
RejectChanges()
End Sub
Private Sub ButtonSave_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ButtonSave.Click
'Update the database and close.
Me.Validate()
Me.OrdersBindingSource.EndEdit()
If Not Me.OrdersTableAdapter.Update(Me.NorthwindDataset.Orders) > 0 _
Then
MsgBox("No records were updated.")
End If
Me.Close()
End Sub
Public Sub New(ByVal nwDataSet As NorthwindDataSet, _
ByVal position As Integer)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' We will specify the DataSet from the DataGrid form here
Me.NorthwindDataset = nwDataSet
Me.OrdersBindingSource.DataSource = Me.NorthwindDataSet
Me.CustomersBindingSource.DataSource = Me.NorthwindDataSet
Me.EmployeesBindingSource.DataSource = Me.NorthwindDataSet
Me.OrdersBindingSource.Position = position
End Sub
Private Sub Order_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'Do not refill. You could optionally refresh this record or
'access additional database columns by creating another query
'with a primary key parameter in the table adapter.
'Me.OrdersTableAdapter.Fill(Me.nwDataSet.Orders)
End Sub
In Sub New, we will pass in the dataset created and filled with the grid. We then point the local NorthwindDataSet variable to the existing dataset. This does not change the properties already set in InitializeComponent for the DataSet, so we must reconfigure the other elements on the form to point to the existing DataSet. Fortunately, the BindingSources provide abstraction. We need only reset the 3 BindingSources' DataSource properties. We then set the BindingSource position so the form knows which record to edit.
dataset is shared with the grid. Orders form in code view. RowHeaderMouseDoubleClick event handler.

FormClosing event handler. Private Sub Orders_FormClosing(ByVal sender As Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
Me.OrdersDataGridView.Dispose()
End Sub
Private Sub Orders_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the
''NorthwindDataSet.Employees' table.
'You can move, or remove it, as needed.
Me.EmployeesTableAdapter.Fill(Me.NorthwindDataSet.Employees)
'TODO: This line of code loads data into the
''NorthwindDataSet.Customers' table.
'You can move, or remove it, as needed.
Me.CustomersTableAdapter.Fill(Me.NorthwindDataSet.Customers)
'TODO: This line of code loads data into the
''NorthwindDataSet.Orders' table.
'You can move, or remove it, as needed.
Me.OrdersTableAdapter.Fill(Me.NorthwindDataSet.Orders)
End Sub
Private Sub OrdersDataGridView_RowHeaderMouseDoubleClick_
(ByVal sender As Object, ByVal e _
As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
Handles OrdersDataGridView.RowHeaderMouseDoubleClick
If Me.OrdersDataGridView.SelectedRows.Count > 0 Then
Dim editForm As New Order(Me.NorthwindDataSet, _
Me.NorthwindDataSet.Orders.Rows.IndexOf_
(CType(CType(Me.OrdersDataGridView.SelectedRows(0)._
DataBoundItem, DataRowView).Row, NorthwindDataSet.OrdersRow)))
editForm.Show()
End If
End Sub
The DataSet is filled on form load. The tableadapter fill statements were created by configuring the BindingSource. We have to dispose of the grid before the form closes to avoid possible DataErrors from the GridViewComboBoxColumns.
The Double-click event instantiates the detail edit form, passing in the DataSet and the position. The position is found by tracking the selected row back to its row instance in the order table, then finding the index of that row in the dataset.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Jul 2007 Editor: Sean Ewington |
Copyright 2007 by Robert R Freeman Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |