

Bind the details data connector to the master data connector, MasterBindingSource.DataMember = "Customers" Bind the master data connector to the Customers table. Establish a relationship between the two tables.ĭataRelation relation = new DataRelation("CustomersOrders",ĭ,ĭ) SqlDataAdapter("select * from Orders", connection) Add data from the Orders table to the DataSet. MasterDataAdapter.Fill(data, "Customers") SqlDataAdapter("select * from Customers", connection) Add data from the Customers table to the DataSet. SqlConnection connection = new SqlConnection(connectionString) ĭata.Locale = "Initial Catalog=Northwind Data Source=localhost" "Integrated Security=SSPI Persist Security Info=False " + valid connection string for a Northwind SQL Server sample For more information, see Protecting Connection Information. Using Windows Authentication (also known as integrated security) is a more secure way to control access to a database. Storing sensitive information, such as a password, within the connection string can affect the security of your application. Be sure to set the connectionString variable to a value that is appropriate for your database. This example uses a GetData method that populates a DataSet object, adds a DataRelation object to the data set, and binds the BindingSource components. Implement a method in your form's class definition for handling the detail of connecting to the database.

Me.Text = "DataGridView master/detail demo" SplitContainer1.Orientation = Orientation.Horizontal Private detailsBindingSource As New BindingSource()ĭetailsDataGridView.Dock = DockStyle.Fillĭim splitContainer1 As New SplitContainer() Private detailsDataGridView As New DataGridView() Private masterBindingSource As New BindingSource() Private masterDataGridView As New DataGridView() This.Text = "DataGridView master/detail demo" This.Load += new System.EventHandler(Form1_Load) SplitContainer splitContainer1 = new SplitContainer() MasterDataGridView.Dock = DockStyle.Fill ĭetailsDataGridView.Dock = DockStyle.Fill Private BindingSource detailsBindingSource = new BindingSource() Private DataGridView detailsDataGridView = new DataGridView() Private BindingSource masterBindingSource = new BindingSource() Private DataGridView masterDataGridView = new DataGridView()

#Xtragrid groups like windows grid code#
If you use the Visual Studio designer to create your form, you can use the designer generated code instead of this code, but be sure to use the names shown in the variable declarations here. The following code provides basic form initialization and includes a Main method. Access to a server that has the Northwind SQL Server sample database.Ĭreating the form To create a master/detail formĬreate a class that derives from Form and contains two DataGridView controls and two BindingSource components.In order to complete this walkthrough, you will need: To copy the code in this topic as a single listing, see How to: Create a Master/Detail Form Using Two Windows Forms DataGridView Controls. When you are finished, you will have a form that shows all the customers in the database in the master DataGridView and all the orders for the selected customer in the detail DataGridView. The form will show two related tables in the Northwind SQL Server sample database: Customers and Orders. In this walkthrough, you will build the form using two DataGridView controls and two BindingSource components. Implementing a master/detail form is easy using the interaction between the DataGridView control and the BindingSource component.
#Xtragrid groups like windows grid update#
Selecting rows in the master table causes the detail table to update with the corresponding child data. One of the most common scenarios for using the DataGridView control is the master/detail form, in which a parent/child relationship between two database tables is displayed.
