Nested DataGrid for Windows Forms?

J

Jabco

I need a grid that displays hierachical related tables. The grid's
DataSource is a dataset. Maybe there is a better way I don't know. There
are 2 tables in the dataset. They have a Relations on "ID" column. I got
the first table in grid displayed ok.

But the problem is when I try to navigate to the child table on grid by
expand and click on the "Relation1" link, it shows the child's
columnheaders but not displaying the child table data, and seems it
creates a new record each time.

Here is my code:

Private custDS As DataSet = New DataSet("Transaction")

Private Sub BindTableToDataGrid()
Dim dt As DataTable
Dim co As DataColumn
dt = custDS.Tables.Add("Trans")

co = dt.Columns.Add("AutoID", GetType(Integer))
co.AutoIncrement = True
co.AutoIncrementSeed = 1
co.AutoIncrementStep = 1
co.ColumnName = "AutoID"
co.ReadOnly = True

co = dt.Columns.Add("ID", GetType(String))
co.ReadOnly = True
'dt.PrimaryKey = New DataColumn() {co}

co = dt.Columns.Add("TransDate", GetType(Date))
co.ReadOnly = True
'...
dt.DefaultView.AllowNew = False


dt = custDS.Tables.Add("TransDetail")
co = dt.Columns.Add("AutoID", GetType(Integer))
co.AutoIncrement = True
co.AutoIncrementSeed = 1
co.AutoIncrementStep = 1
co.ColumnName = "AutoID"
co.ReadOnly = True

co = dt.Columns.Add("ID", GetType(String))
co.ReadOnly = True

co = dt.Columns.Add("ItemName", GetType(String))
co.ReadOnly = True
'...
dt.DefaultView.AllowNew = False

custDS.Relations.Add("Relation1", _
custDS.Tables("Trans").Columns("ID"), _
custDS.Tables("TransDetail").Columns("ID"))

DataGrid1.DataSource = custDS
DataGrid1.DataMember = "Trans"
End Sub

'load data....

Private Sub DataGrid1_Navigate(ByVal sender As System.Object, ByVal ne
As System.Windows.Forms.NavigateEventArgs) Handles DataGrid1.Navigate
Console.WriteLine(DataGrid1.DataMember)
End Sub

Thanks for any help!

Are there any sample code around?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top