In our application we make quite a bit of use of your nested Radgrids, where we must be able to delete each element of the Grid (both parent and children) independently. We have logic that will delete the parent along with all of its children. This is invoked by way of a GridButtonColumn (like the one listed below) which is defined in the parent grid.
Within the parent Grid we have the command:
OnDeleteCommand="WBSGrid_DeleteCommand"
The above method is invoked successfully when we click the GridButtonColumn in the Parent Grid.
Within the GridTableView object contained within the DetailTables portion of the (child) grid we have the following entry:
<telerik:GridButtonColumn
ConfirmText="Delete this Task?"
ConfirmDialogType="RadWindow"
ButtonType="ImageButton"
ImageUrl="~/images/Delete.gif"
ConfirmTitle="Delete"
CommandName="DeleteOption" Text="Delete this Task"
UniqueName="DeleteOptionColumn"
HeaderText = "Delete">
<HeaderStyle Width="35px" HorizontalAlign="Center"/>
<ItemStyle HorizontalAlign="Center" />
</telerik:GridButtonColumn>
When we click one of the Delete Buttons in our child grid, a RadWindow pops up asking us if we wish to delete the entry. So far so good. But if we choose to delete, nothing else happens. Unlike the parent grid, where the OnDeleteCommand element is available to us, there seems to be no corresponding element for the nested grid.
And, if we do find out how to ‘hook up’ our delete button to a code-behind method, we are concerned that it may be awkward to obtain the correct key from the nested grid.
Do you have any suggestions?
9 Answers, 1 is accepted
Hi:
I’ve done what you suggested and it worked very well. Now we are able to delete either ‘child’ rows or ‘ parent’ rows by clicking on their respective buttons. This is, in large part the behavior we had hoped to achieve and we want to thank you for your help in getting there.
Unfortunately, however there is one more feature that we need to enable which is still not quite working… close – but not quite.
Let’s say that we have a ‘parent’-‘child’ pair in our grid that has just one ‘child’ for that ‘parent’. If the user should click on the delete button associated with the ‘child’ we wish to delete both the ‘child’ and the ‘parent’. By doing this we enforce a vital rule in our system that every ‘parent’ in this grid must have at least one ‘child’.
We have developed code that obtains the appropriate keys (‘child’ and associated ‘parent’), queries the database to determine if this is the only child, and if so, issue a call to the database to delete the parent. All of this codes works and the deletion (of ‘parent’ and ‘child’) does take place. The code follows:
protected void WBSGrid_DeleteCommand(object source, GridCommandEventArgs e)
{
WBS_Biz biz = new WBS_Biz(BOEUser);
//Get the GridEditableItem of the RadGrid
GridEditableItem editedItem = (GridEditableItem)e.Item;
string[] DataKeys = editedItem.OwnerTableView.DataKeyNames;
// Passing this test indicates that we are trying to delete a 'parent'
if (DataKeys[0] == "WBSID")
{
//Get the primary key value using the DataKeyValue.
Int32 wbsID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["WBSID"].ToString());
try
{
biz.DeleteSingleWBS(wbsID);
}
catch (Exception ex)
{
}
}
else
{
// falling thru to here means that we are trying to delete a 'child'. When doing so we need to check and see if this
// 'child' is the only one associated with this 'parent'. If so, we need to delete the 'parent' along with the child.
// get the key of the 'child'...
Int32 taskID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["TaskID"].ToString());
// get the 'parent' GridDataItem...
GridDataItem parentItem = (GridDataItem)(e.Item.OwnerTableView.ParentItem);
if (parentItem != null)
{
// get the key of the 'parent'...
Int32 wbsID = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["WBSID"]);
WBS_TaskData biz2 = new WBS_TaskData();
DataTable dt = new DataTable();
try
{
dt = biz2.GetTaskCountByWBS(wbsID);
// if the next test returns true, this is the only 'child' for this 'parent' - delete 'parent'...
if (dt.Rows.Count == 1)
{
biz.DeleteSingleWBS(wbsID);
WBSGrid.Rebind(); // seems to be needed, grid does not refresh... but this line creates error!!!
}
else
{
// otherwise delete 'child'.
biz.DeleteSingleTask(taskID);
}
}
catch (Exception ex)
{
}
}
}
}
When we (successfully) delete a ‘parent’ in this way (delete ‘parent’… in the code) we discover that the grid does not refresh as it does in all other situations. We tried to force the Rebind by including the WBSGrid.Rebind(); statement. But when we include this line we get the following error message:
There was a problem extracting the DataKeyValues from the DataSource. Please ensure that the
DataKeyNames are specified correctly and all fields specicied exist within the datasource.
Any suggestions?
What I would suggest you is to try firing DeleteCommand for the 'parent' row instead of deleting it manually. So instead of the below code:
if (dt.Rows.Count == 1) |
{ |
biz.DeleteSingleWBS(wbsID); |
WBSGrid.Rebind(); // seems to be needed, grid does not refresh... but this line creates error!!! |
} |
Try using the FireCommand method to force parent item deletion.
Check it out and let me know if this works for you.
Best wishes,
Iana
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
As always, your assistance is much appreciated.
I changed the code to read as follows:
if (dt.Rows.Count == 1)
{
parentItem.FireCommandEvent("Delete", String.Empty);
}
The new code does delete the parent and the child. This is desirable. Then it tries to rebind the grid, also desirable. But at some point along the way it raises the following error:
Error: Sys.Webforms.PageRequestManagerServerErrorException: There was a problem extracting DatakeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource.
This error seems to come from a JavaScript module called ‘ScriptResource.axd’.
I have tried to step through the code to see if I could isolate the problem further but I had no luck.
So your suggestion worked about as well as what we had been doing before but, unfortunately we still have the same problem.
Any thoughts?
Could you try canceling the default command running when firing the parentItem deletion:
if (dt.Rows.Count == 1) |
{ |
e.Canceled = true; |
parentItem.FireCommandEvent("Delete", String.Empty); |
} |
See if ti makes any difference.
Greetings,
Iana
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
It works beautifully now.
Thanks so much for your help.
I have a checkbox present inside the header template of RadGrid and there are no other checkboxes present inside item template. When I check the checkbox all items in RadGrid get selected and then I click on delete button will delete the selected item from the grid. My delete button also not present inside any item template.
Could anybody help me.
Try the following approach to achieve the desired scenario.
ASPX:
<telerik:GridTemplateColumn UniqueName="SelectCol" HeaderText="Select" > |
<HeaderTemplate> |
<asp:CheckBox ID="CheckBox1" AutoPostBack="true" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" /> |
</HeaderTemplate> |
</telerik:GridTemplateColumn> |
// To select all the rows on checking the checkbox
CS:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e) |
{ |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
item.Selected = true; |
} |
} |
// To delete the selected rows
CS:
protected void Button1_Click(object sender, EventArgs e) |
{ |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
if (item.Selected) |
{ |
//code to delete the selected rows |
} |
} |
} |
Thanks
Shinu.