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

Acess radgrid nestedviewtemplate

3 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carlos
Top achievements
Rank 1
Carlos asked on 21 Oct 2013, 02:23 PM
Hi all how do i acess a ragrid inside a NestedViewTemplate

Best Regardas.

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 22 Oct 2013, 04:29 AM
Hi Carlos,

Please try the sample code snippet to access a radgrid inside NestedViewTemplate.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
    AllowPaging="true" OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView DataKeyNames="CustomerID">
        <NestedViewTemplate>
            <telerik:RadGrid ID="RadGrid2" runat="server" OnNeedDataSource="RadGrid2_NeedDataSource" />
        </NestedViewTemplate>
    </MasterTableView>
</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();
    }
}
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
0
Carlos
Top achievements
Rank 1
answered on 22 Oct 2013, 02:47 PM
that resolved thanks, i was putting in itemcreated not itemcommand.

But i have another problem, i tried many way hide on columns in the table and im not getting nothing even if i try to count the columns it gieves 0, how can i hide a column in the parent and child grid.

Best Regards
0
Princy
Top achievements
Rank 2
answered on 23 Oct 2013, 04:32 AM
Hi Carlos,

In order to hide a column in the Parent grid you can try the following code snippet:
C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
  RadGrid1.MasterTableView.GetColumnSafe("CustomerID").Visible = false;          
}

In order to hide a column in the nested view grid,please try the following:
C#:
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();
   grid.MasterTableView.GetColumnSafe("OrderID").Visible = false;
 }   
}

Thanks,
Princy


Tags
Grid
Asked by
Carlos
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Carlos
Top achievements
Rank 1
Share this question
or