Choose columns from outside the Grid

3 posts, 0 answers
  1. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 27 Jun 2014 Link to this post

    Hi all,

    I have a pretty large RadGrid with a lot of columns. I'd like to have a column picker outside the grid itself, so that I can show that to the visitor in a more structured way. I know I can enable a ContextMenu for the Grid, but that is not the way I want the user to interact, since it is not that nice (a really long  list with checkboxes).

    How can I bind e.g. a CheckboxList or some other control to the available columns on the Grid and hide or show them?

    Best,
    Daniel
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Jun 2014 in reply to Daniel Plomp Link to this post

    Hi Daniel,

    If you want to have a CheckboxList to show and hide column in the Grid, on its OnLoad, you can bind the columns and on its OnSelectedIndexChanged event you can show and hide the column accordingly. Please take a look at the below code snippet.

    ASPX:
    <asp:CheckBoxList ID="chkboxlistColumns" runat="server" AutoPostBack="true" OnSelectedIndexChanged="chkboxlistColumns_SelectedIndexChanged" OnLoad="chkboxlistColumns_Load">
    </asp:CheckBoxList>

    C#:
    protected void chkboxlistColumns_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        //Loop through the columns and add the items to CheckBoxList
        foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
        {
         chkboxlistColumns.Items.Add(col.UniqueName);               
        }
      }
    }
    protected void chkboxlistColumns_SelectedIndexChanged(object sender, EventArgs e)
    {
      CheckBoxList chkboxlistColumns = (CheckBoxList)sender;
      foreach (ListItem columns in chkboxlistColumns.Items)
      {
      //Apply your logic
      //Not checked columns are shown here
        if (!columns.Selected)
        {
          RadGrid1.MasterTableView.GetColumn(columns.Text).Visible = true;
          RadGrid1.Rebind();
        }
        else
        {
         RadGrid1.MasterTableView.GetColumn(columns.Text).Visible = false;
        }
      }
    }

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Daniel Plomp
    Daniel Plomp avatar
    130 posts
    Member since:
    Feb 2004

    Posted 27 Jun 2014 in reply to Princy Link to this post

    Hi Princy,

    Thanks for the reply.
    Yes, I think that is a nice way to do it from server-side code. I'm actually looking to do this client-side. I came across this demo (http://demos.telerik.com/aspnet-ajax/grid/examples/client/clientsideapi/defaultcs.aspx) and I think I can figure it out.

    Thanks anyway,
    Daniel
Back to Top