Home › Forums › WinForms controls › Xceed Grid for WinForms › Child list for field Master_Detail cannot be created
-
AuthorPosts
-
#16300 |
I am trying to create a dataset/datatables/datarelation/master-detail xceed grid entirely via code without using the designers. I have been able to create the dataset, datatables and relation successfully. I can navigate the tables using the relation so I believe its set up correctly. My problem comes when I call grid.DetailGridTemplates.Add to add my detail grid to my grid. I get an exception stating “Child list for field Master_Detail cannot be created”. Master_Detail is the name of the relation. Can someone point out what I’m doing wrong? I’ve posted my code below.
Dim ds As New DataSet
Dim dtMaster As New DataTable(“Master”)
Dim dtDetail As New DataTable(“Detail”)ds.Tables.Add(dtMaster)
ds.Tables.Add(dtDetail)
dtMaster.Columns.Add(“OrderID”, System.Type.GetType(“System.Int32”))
dtMaster.Columns.Add(“Name”, System.Type.GetType(“System.String”))
dtDetail.Columns.Add(“OrderID”, System.Type.GetType(“System.Int32”))
dtDetail.Columns.Add(“StockNr”, System.Type.GetType(“System.String”))
dtDetail.Columns.Add(“Description”, System.Type.GetType(“System.String”))
Dim rel As New DataRelation(“Master_Detail”, ds.Tables(0).Columns(“OrderID”), ds.Tables(1).Columns(“OrderID”))
ds.Relations.Add(rel)Dim row As System.Data.DataRow = dtMaster.NewRow
row.Item(“OrderID”) = 1
row.Item(“Name”) = “Kevin”
dtMaster.Rows.Add(row)
row = dtMaster.NewRow
row.Item(“OrderID”) = 2
row.Item(“Name”) = “Judy”
dtMaster.Rows.Add(row)row = dtDetail.NewRow
row.Item(“OrderID”) = 1
row.Item(“StockNr”) = “1A”
row.Item(“Description”) = “Test 1A”
dtDetail.Rows.Add(row)
row = dtDetail.NewRow
row.Item(“OrderID”) = 2
row.Item(“StockNr”) = “2A”
row.Item(“Description”) = “Test 2A”
dtDetail.Rows.Add(row)
row = dtDetail.NewRow
row.Item(“OrderID”) = 2
row.Item(“StockNr”) = “2B”
row.Item(“Description”) = “Test 2B”
dtDetail.Rows.Add(row)Dim grid As New GridControl
grid.BeginInit()
Panel1.Controls.Add(grid)
grid.Dock = DockStyle.Fill
grid.DataSource = ds
grid.DataMember = “Master”Dim det As New DetailGrid
det.HeaderRows.Add(New ColumnManagerRow)
det.DataSource = ds
det.DataMember = ds.Relations(0).RelationName
grid.DetailGridTemplates.Add(det)
grid.EndInit()Imported from legacy forums. Posted by Kevin (had 1771 views)
The following thread explains how to do this : http://xceed.com/CS/forums/thread/12723.aspx
The problem in your case is the way you set the data binding to the grids. You need to set the DataSource to the table, and not only to the DataSet, because the DetailGrid will not be able to find the table it needs to bind to for the relation to work.
e.g. :
grid.SetDataBindings( ds.Tables(0), “” )
det.SetDataBindings( ds.Tables(0), ds.Relation(0).RelationName )
Imported from legacy forums. Posted by André (had 1590 views)
-
AuthorPosts
- You must be logged in to reply to this topic.