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

Populate checkbox in Edit Mode

4 Answers 182 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 13 Sep 2012, 10:41 PM
I've got a Radgrid with a single child table that is built programmatically on PageInit. I used your demo here as my guide. The master table cannot be edited, but you can insert new values into the child table. One of the columns in my master table is a checkbox column. I would like to pre-populate a child table checkbox when in edit mode depending on whether the master table column is checked or not. I'm looking at doing it in the EditMode_ItemDataBound event but I can't seem to get anything to stick.  Any suggestions?

4 Answers, 1 is accepted

Sort by
0
Josh
Top achievements
Rank 1
answered on 17 Sep 2012, 07:03 PM
I've been fumbling around with this a bit more, and I still can't get it to behave. To reiterate, I have a nested table that allows inserts into the child table. I want to look at a column in the master table and see if it is checked, if it is checked I want to check the checkbox when in Edit Mode (Inserting a new record).  

My problem is that i can't access the checkbox control in edit mode. The column type is a GridDropDownColumn. No matter what I do I can't get anything other than null from e.Item. I've tried casting e.Item as GridEditFormItem and GridEditFormInsertItem to no avail.

Here's some snippets of things I've tried that definitely don't work... All these try to do is find a control, I'll worry about checking the master to see if the column is checked once I can access the control!

protected void EditMode_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridEditableItem && e.Item.IsInEditMode)
           {
             GridEditFormInsertItem insertItem = e.Item as GridEditFormInsertItem;
             CheckBox ckbx = (CheckBox) insertItem.EditFormCell.Controls[1];
           }
      }
protected void EditMode_ItemDataBound(object sender, GridItemEventArgs e)
    {
       if (e.Item is GridEditableItem && e.Item.IsInEditMode)
         {
          foreach (GridEditFormInsertItem childeditItem in RadGrid1.MasterTableView.GetItems(GridItemType.EditItem)) 
          {
          CheckBox chk = (CheckBox)childeditItem.FindControl("isOffline");
          }
        }
   }
GridEditFormItem item = (GridEditFormItem)e.Item;
CheckBox ck = (CheckBox)item.FindControl("isOffline");
ck.Checked = true;

Any thoughts?
0
Accepted
Shinu
Top achievements
Rank 2
answered on 18 Sep 2012, 08:40 AM
Hi,

I guess your requirement is to pre-populate the GridCheckBoxColumn in child radgrid on inserting a new row depending on the Parent row's GridCheckBoxColumn. Here is the Sample code snippet I tried.

C#:
GridTableView tableViewOrders = new GridTableView(RadGrid1);
tableViewOrders.DataSourceID = "SqlDataSource2";
tableViewOrders.Name = "Child"// gave name for the detail table
. . .
. . .
 
void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
        if (e.Item is GridEditableItem && e.Item.OwnerTableView.IsItemInserted && e.Item.OwnerTableView.Name=="Child")
        {  
        GridEditableItem insertItem = e.Item as GridEditableItem;
        CheckBox child = (CheckBox)insertItem["UniqueNameOfChildCheckBox"].Controls[0]; // accessing the child GridCheckboxColumn
        GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
        CheckBox parent = (CheckBox)parentItem["UniqueNameOfParentCheckBox"].Controls[0]; //accessing the parent GridCheckboxColumn
        if (parent.Checked == true)
        {
            child.Checked = true;
        }
    }
}

Thanks,
Shinu.
0
Andrey
Telerik team
answered on 18 Sep 2012, 02:22 PM
Hello,

I have answered your query in the support thread you have opened, but I will post the answer here as well:

protected void EditMode_ItemDataBound(object sender, GridItemEventArgs e)
{
//Grabs DataKey from Master Table
    GridDataItem parentItem = (GridDataItem)(e.Item.OwnerTableView.ParentItem);
    int index = Convert.ToInt32((parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["IssueID"]));
    Session["CurrentIssueID"] = index;
       
       
    //This is where I am having issues accessing the Checkbox
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        try
        {
    //This doesn't work
            GridEditFormItem editedItem = e.Item as GridEditFormItem;
            CheckBox ck = (CheckBox)editedItem["State"].Controls[0];
  
    //Or this
            GridEditFormInsertItem insertItem = e.Item as GridEditFormInsertItem;
            CheckBox ckbx = (CheckBox)CheckBox)editedItem["State"].Controls[0];
            ckbx.Checked = true;
      
        catch { }
    }
  
}


All the best,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Josh
Top achievements
Rank 1
answered on 19 Sep 2012, 05:21 PM
Thank you to both Andrey and Shinu for helping me get through this. The solution closely resemebled Shinu's post, but there was one caveat that I didn't disclose in the thread...Since I am building this radgrid programmatically on PageInit, i have to define the Event Handlers and I made a silly mistake. I was using ItemEvent instead of ItemDataBound. Once I fixed that, e.Item was able to cast without throwing a null exception.

//Bad! ItemEvent causes e.Item to return as a null reference every time
RadGrid1.ItemEvent+= new GridItemEventHandler(RadGrid1_DB);
 
//Good! This should be firing on the ItemDataBound Event
RadGrid1.ItemDataBound += new GridItemEventHandler(RadGrid1_DB);

Thanks again for your help guys!
Tags
Grid
Asked by
Josh
Top achievements
Rank 1
Answers by
Josh
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Andrey
Telerik team
Share this question
or