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

[Solved] detailtable rows.count = 0 problem

10 Answers 366 Views
Grid
This is a migrated thread and some comments may be shown as answers.
sf
Top achievements
Rank 1
sf asked on 24 Jul 2009, 04:33 AM
I have a databond radgrid and a databond detailtable within the radgrid. In the detailtable I have an extra checkbox column which is not databound.

When I click on a commandButton on a mastertable row I want to find the checkbox control within the detailtable of that mastertable row, and store the checkbox "Checked" value of each detailtable row somewhere else.

I use the following code to locate the detail table

if (e.CommandName == "TestCommand"
{
GridTableView gtv = (GridTableView)e.Item.OwnerTableView;
GridTableView gtvContents = (GridTableView)gtv.DetailTables[0];

When I was trying to loop through the detailtable to find the checkbox i realised that the row count is 0, which is incorrect, therefore i will not be able to find the checkbox control....

please help, thanks in advance

10 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 24 Jul 2009, 07:03 AM
Hi,

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.
0
sf
Top achievements
Rank 1
answered on 24 Jul 2009, 08:30 AM
thanks Princy very much for your help, it worked well.

So "expand" is actually required for populating the detailtable, therefore if not expanded, the detailtable will contain nothing, right?
0
Saravanan Kannan
Top achievements
Rank 1
answered on 03 Aug 2009, 10:15 AM
Hi 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.

0
Princy
Top achievements
Rank 2
answered on 03 Aug 2009, 11:17 AM
Hello Saravanan,

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.
0
Saravanan Kannan
Top achievements
Rank 1
answered on 05 Aug 2009, 07:19 AM
Hi 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
0
Shinu
Top achievements
Rank 2
answered on 05 Aug 2009, 10:37 AM
Hello Saravanan,

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">        

c#:
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; 
        } 
    } 

Thanks
Shinu.
0
Saravanan Kannan
Top achievements
Rank 1
answered on 05 Aug 2009, 11:17 AM

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

 

0
Princy
Top achievements
Rank 2
answered on 05 Aug 2009, 02:02 PM
Hello Saravanan,

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.
0
Saravanan Kannan
Top achievements
Rank 1
answered on 05 Aug 2009, 02:58 PM
Hi Princy,

Thks for your help, its working fine.

Saravanan
0
Saravanan Kannan
Top achievements
Rank 1
answered on 17 Aug 2009, 05:35 PM
Hi Princy,

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

 

 

Tags
Grid
Asked by
sf
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
sf
Top achievements
Rank 1
Saravanan Kannan
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or