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

Parent Grid Rebind

4 Answers 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rick Atkinson
Top achievements
Rank 1
Rick Atkinson asked on 21 Jan 2013, 06:15 PM
I have a master detail grid.  For the master grid, I use NeedDataSource.  In addtion, on the ItemDatabound event, I modify some of the columns (ex. I hide some of the command buttons for some rows).

When a row is expanded and DetailTableDataBind is fired, the detail table renders fine.  However, the modifications made to the parent table above via ItemDatabound are lost.  It is as if I need to rebind my parent table.  Have been unable to do that, though without losing my detail table view.

I posted the relevant code below. Any help would be greatly appreciated.  Thanks, Rick

void afterGrid_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
    var dataItem = e.DetailTableView.ParentItem;
     
    var memberId = dataItem.GetDataKeyValue("Id").ToString().ToInteger();
 
    using (var db = new DataContext())
    {
        var toBind = //retrieves the data
 
        e.DetailTableView.DataSource = toBind;
    }
}
 
void afterGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    //Answers is name of detail data
    if (e.Item is GridDataItem && e.Item.OwnerTableView.Name != "Answers")
    {
        var item = (GridDataItem)e.Item;
        var cell = item["Remind"];
 
        if (cell.IsNotNull())
        {   //dont send if already submitted
            var data = (ReviewMember)item.DataItem;
 
            if (data.SubmitDate.HasValue)
            {
                cell.Controls.Clear();
           }
            else if (data.LastSavedDate.HasValue )
            {
                cell.Controls.Clear();
                cell.Text = "Sent";
            }
        }
    }
    else if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Answers")
    {
        var item = (GridDataItem)e.Item;
        var data = (MemberResult)item.DataItem;
        var cell = item["Attach"];
 
        if (data.Question.Type != QuestionTypes.Upload)
        {
            cell.Controls.Clear();
        }
    }
}
 
void afterGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName.Equals("Remind"))
    {
       //do some work       
       afterGrid.Rebind();
    }
    else if (e.CommandName == RadGrid.ExpandCollapseCommandName)
    {
        foreach (GridItem item in e.Item.OwnerTableView.Items)
        {
            if (item.Expanded && item != e.Item)
            {
                item.Expanded = false;
            }
        }
    }
}

4 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 22 Jan 2013, 04:55 AM
Hello,

Add your logic in Prerender event.

Please check below code snippet in this i have moved my itemdatabound's event code in Prerender event.

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem && e.Item.OwnerTableView.Name == "Parent")
       {
           GridDataItem item = e.Item as GridDataItem;
 
           var cell = item["CustomCommand"];
 
           if (item.ItemIndex % 2 == 0)
           {
               cell.Controls.Clear();
           }
       }
   }



protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
       {
           var cell = item["CustomCommand"];
 
           if (item.ItemIndex % 2 == 0)
           {
               cell.Controls.Clear();
           }
       }
   }


Thanks,
Jayesh Goyani
0
Rick Atkinson
Top achievements
Rank 1
answered on 23 Jan 2013, 02:55 PM
Thanks Jayesh.  However, I still need access to the GridItemEventArgs of ItemDataBound.  I need to read certain properties of the data row object to determine if I should hide the command icon.

I may be able to work around it via PreRender.  But, without access to my data row, it will not be nearly as elegant.

Thanks, 

Rick
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 23 Jan 2013, 05:49 PM
Hello,

please try with below code snippet.

protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
       {
  
           string ID = item.GetDataKeyValue("ID").ToString(); // Access DataItem by using DatakeyNames
           string Name = item.GetDataKeyValue("Name").ToString(); // Access DataItem by using DatakeyNames
 
           var cell = item["CustomCommand"];
  
           if (item.ItemIndex % 2 == 0)
           {
               cell.Controls.Clear();
           }
       }
   }
<MasterTableView  DataKeyNames="ID,Name"  >


Thanks,
Jayesh Goyani
0
Rick Atkinson
Top achievements
Rank 1
answered on 24 Jan 2013, 02:43 AM
Good idea, that worked.  Thanks Jayesh.
Tags
Grid
Asked by
Rick Atkinson
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Rick Atkinson
Top achievements
Rank 1
Share this question
or