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

Setting columns based on checkbox selection

2 Answers 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Royal
Top achievements
Rank 1
Royal asked on 31 Aug 2010, 06:29 PM
I am interested in setting the output columns of my RadGrid based on the checked columns (true or false) selected by the user.

As an example, lets say I have ten default columns returned to my datagrid, but the user only wants to display 5 of those. Can anyone point me in the right direction?

Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 01 Sep 2010, 07:35 AM
Hello Royal,

If you want to customize the grid column visibility based on the checkbox selection, then you can either use HeaderContextMenu option or code behind approach.

1) From aspx page
Simply set EnableHeaderContextMenu property to "True", which allows show/hide the grid columns based on user preferences.

Mark-up:

<
telerik:RadGrid EnableHeaderContextMenu="true" ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource"
               OnUpdateCommand="RadGrid1_UpdateCommand" OnPreRender="RadGrid1_PreRender">

2)From code behind:
If the CheckBoxes are placed outside grid, then set the column visibility in PreRender event by using following code snippet.

Code behind:
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       RadGrid1.MasterTableView.GetColumn("EmployeeID").Visible = CheckBox1.Checked;
       RadGrid1.MasterTableView.GetColumn("FirstName").Visible = CheckBox2.Checked;
       RadGrid1.Rebind();
   }


Regards,
Shinu.
0
Royal
Top achievements
Rank 1
answered on 01 Sep 2010, 05:05 PM
Awesome, thank you!
Tags
Grid
Asked by
Royal
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Royal
Top achievements
Rank 1
Share this question
or