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

[Solved] How can i acces the NestedViewTemplate data with a button

1 Answer 170 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ricardo
Top achievements
Rank 1
Ricardo asked on 20 Jun 2014, 06:40 PM
Morning.

I want to acces the data of a NestedViewTemplate item with a button for each row there, here is a image so i can explain better:

i want to put a button where the red scuare is to acces the data that inside the blue scuare.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 23 Jun 2014, 05:09 AM
Hi Ricardo,

I'm not clear what does your NestedViewTemplate contains. I guess its another RadGrid, you can add a GridButtonColumn to your grid and set its CommandName property and access it in the code behind on OnItemCommand event. Please take a look at the sample code snippet. If this is not the requirement, provide your full code snippet and elaborate on your requirement.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="true" OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView DataKeyNames="CustomerID" Name="Parent" CommandItemDisplay="Top" HierarchyLoadMode="ServerOnDemand">
        <NestedViewTemplate>
            <telerik:RadGrid ID="RadGrid2" runat="server" OnNeedDataSource="RadGrid2_NeedDataSource" OnItemCommand="RadGrid2_ItemCommand" MasterTableView-Name="Child" AutoGenerateColumns="false">
                <MasterTableView>
                    <Columns>
                        <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="EmployeeID" UniqueName="EmployeeID" />
                        <telerik:GridBoundColumn DataField="CustomerID" HeaderText="CustomerID" UniqueName="CustomerID" />
                        <telerik:GridButtonColumn Text="Get Data" CommandName="GetData" UniqueName="ButtonColumn">
                        </telerik:GridButtonColumn>
                    </Columns>
                </MasterTableView>
                <ClientSettings Selecting-AllowRowSelect="true">
                </ClientSettings>
            </telerik:RadGrid>
        </NestedViewTemplate>
    </MasterTableView>
    <ClientSettings AllowExpandCollapse="true" Selecting-AllowRowSelect="true">
    </ClientSettings>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    RadGrid1.DataSource = GetDataTable("SELECT CustomerID, CompanyName, ContactTitle FROM Customers");
}
protected void RadGrid2_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    GridDataItem parentItem = ((sender as RadGrid).NamingContainer as GridNestedViewItem).ParentItem as GridDataItem;
    (sender as RadGrid).DataSource = GetDataTable("SELECT OrderID, EmployeeID, CustomerID FROM Orders where CustomerID='" + parentItem.GetDataKeyValue("CustomerID").ToString() + "'");
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.ExpandCollapseCommandName && !e.Item.Expanded)
    {
        GridDataItem parentItem = e.Item as GridDataItem;
        RadGrid grid = parentItem.ChildItem.FindControl("RadGrid2") as RadGrid;
        grid.Rebind();
    }
  
}
protected void RadGrid2_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "GetData")
    {
        GridDataItem dataItem = e.Item as GridDataItem;
        string ID = dataItem["OrderID"].Text;//Get row value
    }
}
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
 
    DataTable myDataTable = new DataTable();
 
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
 
    return myDataTable;
}

Thanks,
Princy
Tags
Grid
Asked by
Ricardo
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or