Need to hide column in nested grid

2 posts, 0 answers
  1. Brian
    Brian avatar
    5 posts
    Member since:
    Sep 2012

    Posted 29 Aug 2012 Link to this post

    I have a radgrid, which has a nested template view with another grid in the nested view.  I have a column that contains a hyperlink, which i only want displayed if the user is in a particular role.  I have the role part down, but my problem is I cannot access the grid through the vb.net code behind window.

    I try me.gridSchedule, but it is not in the list.

    How can I hide this column programmatically?

    Thanks...Brian
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 29 Aug 2012 Link to this post

    Hello,

    Please try with below code.

    <telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid1_NeedDataSource">
                <MasterTableView DataKeyNames="ID" HierarchyLoadMode="Client">
                    <Columns>
                        <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                        </telerik:GridBoundColumn>
                        <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                        </telerik:GridBoundColumn>
                    </Columns>
                    <NestedViewTemplate>
                        <telerik:RadGrid ID="RadGrid2" runat="server" AutoGenerateColumns="False" OnNeedDataSource="RadGrid2_NeedDataSource"
                         OnPreRender="RadGrid2_PreRender">
                            <MasterTableView>
                                <Columns>
                                    <telerik:GridBoundColumn DataField="ID" HeaderText="ID" UniqueName="ID">
                                    </telerik:GridBoundColumn>
                                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                                    </telerik:GridBoundColumn>
                                </Columns>
                            </MasterTableView>
                        </telerik:RadGrid>
                    </NestedViewTemplate>
                </MasterTableView>
            </telerik:RadGrid>
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
          {
              dynamic data = new[] {
                new { ID = 1, Name ="aaa"},
                new { ID = 2, Name = "bbb"},
                new { ID = 3, Name = "ccc"},
                new { ID = 4, Name = "ddd"},
                 new { ID = 5, Name ="eee"}
              };
              RadGrid1.DataSource = data;
          }
     
          protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
          {
              if (e.Item is GridDataItem)
              {
                  GridDataItem item = e.Item as GridDataItem;
                  RadGrid RadGrid2 = item.FindControl("RadGrid2") as RadGrid;
                  RadGrid2.Rebind();
              }
          }
     
          protected void RadGrid2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
          {
              RadGrid RadGrid2 = sender as RadGrid;
              GridDataItem item = (RadGrid2.NamingContainer as GridNestedViewItem).ParentItem as GridDataItem;
              string strID = item.GetDataKeyValue("ID").ToString();
     
              dynamic data = new[] {
                new { ID = 1, Name ="aaa_"+strID},
                new { ID = 2, Name = "bbb_"+strID},
                new { ID = 3, Name = "ccc_"+strID},
                new { ID = 4, Name = "ddd_"+strID},
                 new { ID = 5, Name ="eee_"+strID}
              };
     
              RadGrid2.DataSource = data;
          }
          protected void RadGrid2_PreRender(object sender, EventArgs e)
          {
              // Do Your logic here
              RadGrid RadGrid2 = sender as RadGrid;
              RadGrid2.MasterTableView.Columns.FindByUniqueName("ID").Visible = false;
          }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top