Home - Forums-.NET - FlyGrid.Net (Windows Forms) - Help with Master/Detail grid

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

Help with Master/Detail grid
Link Posted: 26-May-2006 01:45
I want to get a grid set up and working just like the Master/Detail demo app.  For some reason I just can't get it working correctly.  I went through the source of the demo and picked out what I thought were the pieces I needed to get my own test working.

My dataset has the following 2 tables:
Batch:
    BatchNumber - Int32
    NumberOfItems - Int32
    Amount - Decimal

Item:
    BatchNumber - Int32
    ControlNumber - Int64
    Amount - Decimal


I'm populating the dataset with data, and here is the code I'm using to set up the grid (I'm doing no other setup through Visual Studio 2005's gui):
Private Sub InitGrid()
  Dim col As Column = Nothing

  MyDataSet.Relations.Add(\"Batch_Item\", MyDataSet.Tables(\"Batch\").Columns(\"BatchNumber\"), MyDataSet.Tables(\"Item\").Columns(\"BatchNumber\"))

  For Each dc As DataColumn In MyDataSet.Tables(\"Batch\").Columns
    If dc.ColumnName = \"BatchNumber\" Then
      Dim hc As HierachyColumn
      hc = New HierachyColumn(dc.Caption, dc.ColumnName)
      hc.ShowOutline = True
      hc.TextAlign = ContentAlignment.MiddleRight
      col = hc
    Else
      col = New Column(dc.Caption, dc.ColumnName)
    End If

    FlyGrid1.Columns.Items.Add(col)
  Next

  For Each dc As DataColumn In MyDataSet.Tables(\"Item\").Columns
    col = New Column(dc.Caption, dc.ColumnName)

    FlyGrid1.Columns.NestedColumns.Items.Add(col)
  Next

  FlyGrid1.Rows.DataSource = MyDataSet
  FlyGrid1.Rows.DataMember = \"Batch\"
End Sub


Can anyone shed any light on what I'm missing?
Link Posted: 26-May-2006 04:00
Probably parent column in relation has no unique values,
please set parentColumn.Unique = true.
Below is complete working code:
[VB.NET]
Public Sub New()
  MyBase.New()

  'This call is required by the Windows Form Designer.
  InitializeComponent()

  'Add any initialization after the InitializeComponent() call
  InitGrid()
End Sub

'----Private declarations
Private myDataSet As DataSet
Private mCol As DataColumn
Private dCol As DataColumn

'-----------Preparing Dataset
Private Sub PrepareDataSet()
  myDataSet = New DataSet(\"MasterDetail\")
  '1 table
  Dim mTable As DataTable = New DataTable(\"Master\")
  mCol = New DataColumn(\"BatchNumber\", GetType(System.Int32))
  mCol.Unique = True
  mTable.Columns.Add(mCol)
  mTable.Columns.Add(New DataColumn(\"NumberOfItems\", GetType(System.Int32)))
  mTable.Columns.Add(New DataColumn(\"Amount\", GetType(System.Int32)))
  Dim i As System.Int32
  For i = 0 To 10
    mTable.LoadDataRow(New Object() {i, 12, 15}, True)
  Next i

  '2 table
  Dim dTable As DataTable = New DataTable(\"Detail\")
  dCol = New DataColumn(\"BatchNumber\", GetType(System.Int32))
  dTable.Columns.Add(dCol)
  dTable.Columns.Add(New DataColumn(\"ControlNumber\", GetType(System.Int64)))
  dTable.Columns.Add(New DataColumn(\"Amount\", GetType(System.Int32)))
  Dim j As System.Int32
  For i = 0 To 10
    For j = i + 2 To 12
      dTable.LoadDataRow(New Object() {i, j, 12}, True)
    Next j
  Next i
  'add tables
  myDataSet.Tables.AddRange(New DataTable() {mTable, dTable})
  'add relations
  Dim relation As DataRelation = New DataRelation(\"Master_Detail\", mCol, dCol)
  myDataSet.Relations.Add(relation)
End Sub

'--------------FlyGrid initialization
Private Sub InitGrid()
  'create and init dataset
  PrepareDataSet()
  'add columns to the master .Columns
  Dim col As Column
  For Each dc As DataColumn In myDataSet.Tables(\"Master\").Columns
    'if it is master column - add Hierarchycolumn to show +/- buttons
    If dc Is mCol Then
      Dim hc As HierachyColumn
      hc = New HierachyColumn(dc.Caption)
      hc.ShowOutline = True
      hc.TextAlign = ContentAlignment.MiddleRight
      col = hc
    Else
      col = New Column(dc.Caption)
    End If

    flyGrid1.Columns.Items.Add(col)
  Next
  'add columns to detail columns - .Columns.NestedColumns
  For Each dc As DataColumn In myDataSet.Tables(\"Detail\").Columns
    If (Not dc Is dCol) Then 'skip detail/column
      col = New Column(dc.Caption, dc.ColumnName)
      flyGrid1.Columns.NestedColumns.Items.Add(col)
    End If
  Next
  'bind FlyGrid
  flyGrid1.Rows.DataSource = myDataSet
  flyGrid1.Rows.DataMember = \"Master\"
End Sub
Link Posted: 26-May-2006 05:08
Thanks for your help, I really appreciate it.

It was really strange.  I copy/pasted your code verbatim into my test app and it didn't work.  So I created a new project with your code and it did work.   Hmmmm...odd.  Then I removed the grid from my test app and re-added it and it worked.  It even works with the original code that I posted.  There must have been something set wrong with the grid for it not to work.

Anyway, again, thanks for all your help,
Justin
Link Posted: 26-May-2006 07:42
If you have not worked (old) code or executable, please send us () for analysis,
please zip.
Link Posted: 26-May-2006 07:47
[quote="NineRays"]If you have not worked (old) code or executable, please send us () for analysis,
please zip.
Unfortunately I've already deleted the old code.
Link Posted: 26-May-2006 07:50
May be you've used flyGrid.Rows.VirtualMode = true after FlyGrid binding?