This is a migrated thread and some comments may be shown as answers.

Master and Detail Grids, but using OnNeedDataSource

2 Answers 88 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ronnie
Top achievements
Rank 1
Ronnie asked on 13 Sep 2010, 11:42 PM
Hello,

I'm trying to create a solution similiar to the Master/Detail demo.

I have 2 grids on a single page, MasterGrid and DetailsGrid.
I have AllowRowSelect="true" in MasterGrid and that works fine.
My problem is that I need to get the selected DataKeyValue (of MasterGrid) from within the DetailsGrid_NeedDataSource as a parameter value.

I can get the DataKeyValue from within a grids own GridCommand event, something like:
protected void MasterGrid_DeleteCommand(object source, GridCommandEventArgs e)
{
   GridDataItem dataItem = (GridDataItem)e.Item;
   int id = Convert.ToInt32(dataItem.GetDataKeyValue("ID"));
}

But again, I can't get the value in an unrelated method (NeedDataSource of a different grid).
Any assistance is appreciated.

Thank you,
Ronnie

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 14 Sep 2010, 05:41 AM
Hello Ronnie,

Try the following code snippet in DetailGrid's NeedDataSource event to get the DataKeyValue of selected row from parent grid.

C#:
protected void DetailsGrid_NeedDataSource (object source, GridNeedDataSourceEventArgs e)
   {
       GridDataItem selectedItem = (GridDataItem)MasterGrid.SelectedItems[0];
       int id1 = Convert.ToInt32(selectedItem.GetDataKeyValue("ID"));
   }

Thanks,
Princy.
0
Ronnie
Top achievements
Rank 1
answered on 14 Sep 2010, 08:23 AM
Thank you Princy, this was what I needed.

A couple of additions were required for me to tie it all together.  In my case I had to check if there was a selected item first because when trying to load the page I was get an 'Index was out of range' exception.  And then I had to rebind the DetailsGrid after selecting from the MasterGrid.

Here's the completed example for anyone to reference from:
protected void MasterGrid_SelectedIndexChanged(object sender, EventArgs e)
{
   DetailsGrid.Rebind();
}
  
protected void DetailsGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
   int id = 0;
   if (MasterGrid.SelectedItems.Count > 0)
   {
      GridDataItem selectedItem = (GridDataItem)MasterGrid.SelectedItems[0];
      id = Convert.ToInt32(selectedItem.GetDataKeyValue("ID"));
   }
  
   ProductDetailsList details = new ProductDetailsList();
   details.LoadDetails(id);
   DetailsGrid.DataSource = details;
}

Once again, thank you Princy,
Ronnie
Tags
Grid
Asked by
Ronnie
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Ronnie
Top achievements
Rank 1
Share this question
or