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

Choose columns from outside the Grid

2 Answers 64 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel Plomp
Top achievements
Rank 2
Daniel Plomp asked on 27 Jun 2014, 10:46 AM
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 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 27 Jun 2014, 11:36 AM
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
0
Daniel Plomp
Top achievements
Rank 2
answered on 27 Jun 2014, 11:38 AM
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
Tags
Grid
Asked by
Daniel Plomp
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Daniel Plomp
Top achievements
Rank 2
Share this question
or