10 Answers, 1 is accepted
Here is an example of how to access DetailTable data on clickina a button in the parent row:
aspx:
c#:
| protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) |
| { |
| if (e.CommandName == "TestCommand") |
| { |
| GridDataItem dataItem = (GridDataItem)e.Item; |
| if (dataItem.Expanded) |
| { |
| GridTableView nestedView = (GridTableView)dataItem.ChildItem.NestedTableViews[0]; |
| foreach (GridDataItem item in nestedView.Items) |
| { |
| string checkStatus = ((CheckBox)item.FindControl("CheckBox1")).Checked.ToString(); |
| } |
| } |
| } |
Thanks
Princy.
I had tried ur code but getting some error as follows:
"Unable to cast object of type 'Telerik.WebControls.GridCommandItem' to type 'Telerik.WebControls.GridDataItem'." at this line
GridDataItem dataItem = (GridDataItem)e.Item;
please help, thks in advance.
The above code can be used to retrieve DetailTable data on clicking a button placed in the parent row, whereas if you are trying to access the detailtable data on clicking a button in the command row, you would have to use the following code:
aspx:
| <CommandItemTemplate> |
| <asp:Button ID="Button1" runat="server" CommandName="TestCommand" Text="CommandText" /> |
| </CommandItemTemplate> |
c#:
| protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) |
| { |
| if (e.CommandName == "TestCommand") |
| { |
| foreach (GridDataItem dataItem in RadGrid1.Items) |
| { |
| if (dataItem.Expanded) |
| { |
| GridTableView nestedView = (GridTableView)dataItem.ChildItem.NestedTableViews[0]; |
| foreach (GridDataItem item in nestedView.Items) |
| { |
| string strtxt = item["ColumnUniqueName"].Text; |
| } |
| } |
| } |
| } |
| } |
Thanks
Princy.
thanks for you help, it works for me.
currently i am facing a problem. my scenario is as follows:
i have a radgrid with 1 mastertableview and 3 detailtables with gridtemplatecolumn. in each detailtables i have imagebuttons on commonitemtemplate for adding, editing and deleting records. i want to know if a add button in one of the detailtables in the second row of mastertableview is clicked, then how i can find from which row of mastertableview is called so that i can add new records to that particular detailtables.
thanks in advance
You check for the CommandName of the button anddistinguish between the gridtableview instances using their nameproperties and then access the parent row as shown below:
aspx:
| <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" runat="server" OnItemCommand="RadGrid1_ItemCommand"> |
| <MasterTableView Name="Master"> |
| <DetailTables> |
| <telerik:GridTableView Name="Detail" DataSourceID="SqlDataSource2" CommandItemDisplay="Top"> |
| protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) |
| { |
| if (e.CommandName == "AddCommand" && e.Item.OwnerTableView.Name=="Detail") |
| { |
| GridDataItem parentItem = e.Item.OwnerTableView.ParentItem; |
| string strtxt = parentItem["BoundColumnUniqueName"].Text; |
| } |
| } |
Hi Shinu,
Thks for your reply. I had given like
switch (e.CommandName)
{
case "AddNewRow":
GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
string strtxt = parentItem["DI_SNO"].Text
InsertNewRecord(e);
break;
}
where DI_SNO is a GridTemplateColumn of the MasterTableView and the return value is empty string.
Please help, thks in advance
For TemplateColumns you would have to access the control in the ItemTemplate and then access its text value. Check out the example below:
aspx:
| <telerik:GridTemplateColumn> |
| <ItemTemplate> |
| <asp:TextBox ID="TextBox1" Text="CustomText" runat="server"></asp:TextBox> |
| </ItemTemplate> |
| </telerik:GridTemplateColumn> |
c#:
| protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) |
| { |
| if (e.CommandName == "AddCommand" && e.Item.OwnerTableView.Name=="Detail") |
| { |
| GridDataItem parentItem = e.Item.OwnerTableView.ParentItem; |
| TextBox txtbx = (TextBox)parentItem.FindControl("TextBox1"); |
| string strtxt = txtbx.Text; |
| } |
| } |
Regards
Princy.
Thks for your help, its working fine.
Saravanan
I am having a issue. The scenario is as follows:
I have a Radgrid with a MasterTableView with some textbox and 3 checkboxes in GridTemplateCollumn. There are 3 DetailTables which are controlled by the 3 checkboxes in the MasterTableView. When a checkbox is checked i need to open the corresponding nestedviewtable. In the checkbox checkedchanged event i am trying to expand that particular row as follows:
GridDataItem
dataItem;
CheckBox
chk = (CheckBox)sender;
int parentRowNo = ((GridDataItem)chk.NamingContainer).ItemIndex + 1;
dataItem = radDI.MasterTableView.Items[parentRowNo - 1];
nestedView = dataItem.ChildItem.NestedTableViews[1];
if (chk.Checked)
{
nestedView.DataSource = childFrame();
nestedView.DataBind();
nestedView.Visible =
true;
dataItem.Expanded = true;
}
at the dataItem.Expanded = true; is throwing some error
"There was a problem extracting DataKeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource."
if i comment it there is no error and that row is not expanded. please help, thks in advance.
Note:
this page is opened using window.showModalDialog method.
rgds,
Saravanan