How do I bind two DataGrids in a master-detail relationship?

for Compact Framework 1.0

Answer this question

How do I bind two DataGrids in a master-detail relationship?

  • Joseph Torre

    Hi Ilya,

    I am doing the same as you have written here (the above Visual C# code) BUT it gives me the following error:

    Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'.

    Please how can do this


  • Kirill Osenkov

    I am trying to translate the supplied code to VB (don't ask). I have something like:

    Dim dvPrimary As DataView = DirectCast((Me.dgOrderItems.DataSource.DefaultView), DataTable)

    But the compiler says "Option Strict On disallows late binding." Any ideas on what I am doing wrong

    Do I need to add anything to the designer in Visual Studio to support the child data grid or is it "inherited" from the parent

    Finally, I would like to be able to right-click on a parent row and add a row to the child (and of course have the row refreshed). I am used to seeing something like a context menu event but I don't see that in the data grid. Is this kind of thing handled in a different manor

    Thank you.

    Kevin



  • Fabio Pintos

    Me.dgOrderItems.DataSource is an object, object does not have DefaultView you're trying to use. In C# that would be syntax error (xxx does not implement yyy), but VB would 'believe' you and would try to figure out what this object is at runtime. That constitutes late binding which is disabled by your settings and not supported by NETCF.

    Solution is to explicitly cast objects to whatever they actually are before using anything. It appears you're trying to do that, but you have few brackets wrong:

    Dim dvPrimary As DataView = DirectCast(Me.dgOrderItems.DataSource, DataTable).DefaultView



  • opc3

    Thanks Ilya!
    You answered by my question and i solve this problem too (also usage event CurrentCellChanged and RowFilter).
    I have typed DataSet with table Products (FK - CategoryCode) and Categories (PK-Code)



     

    private void dataGridCategories_CurrentCellChanged(object sender, System.EventArgs e)

       SyncCategoryProducts (); 

    private void SyncCategoryProducts() 
    {  
     BindingManagerBase bm = dataGridPrice.BindingContext [dataGridCategories.DataSource];
     DataRow curRow = (bm.Current as DataRowView).Row;
     string code = curRow["Code"] as string;  
     dataSet.Products.DefaultView.RowFilter = string.Format("CategoryCode = '{0}'",code); 
    }


     


  • Carlos Voltolini Neto

    If by Master-Detail you mean two related tables in the DataSet, you can do it by handing CurrentCellChanged event on a master table's DataGrid.
    In this event you should filter out records to be shown in a second details grid based on a master's current row primary key.

    Sample below assumes you have two DataGrid controls - master is DataGrid1 and details table is DataGrid2.

    These controls are bound to related DataTables called Stores and Items in a DataSet, first one has a primary key column "ID" and a second one has foreign key column "Store_ID".

    As you move to another row in the master's grid, event will fire. Filter will be applied and second grid will show items for selected store only.



     

    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e) {

     

     DataView dvPrimary = ((DataTable)this.dataGrid1.DataSource).DefaultView;

     

     DataView dvSeconday = ((DataTable)this.dataGrid2.DataSource).DefaultView;

     

     DataRowView drv = dvPrimary[this.dataGrid1.CurrentRowIndex];

     

     dvSeconday.RowFilter = String.Format("Store_ID = {0}", drv["ID"] );

     

    }

     


     



  • How do I bind two DataGrids in a master-detail relationship?