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

How to select what columns display in a grid

3 Answers 120 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 28 Oct 2008, 05:45 PM
I want to use the toolbar above the radgrid. In the toolbar i would like to have a list of all the available columns (first name, last name, etc) with check boxes next to each field.

Based on which field is checked, those fields will display in the grid as columns. So if First name and last name are checked, those will be the only columns that display in the grid.

Any examples would be great!

Cheers

3 Answers, 1 is accepted

Sort by
0
Baatezu
Top achievements
Rank 2
answered on 28 Oct 2008, 06:44 PM
RadGrid1.Columns.FindByUniqueName("YOURUNIQUENAMEHERE").Visible = chk.Checked;

When using the Property Builder for the grid there is a property for the columns called 'UniqueName'. Set that to some easily identifiable and then you can find that column using the above method and modify it's settings.

0
Shinu
Top achievements
Rank 2
answered on 29 Oct 2008, 05:49 AM
Hi Brett,

You can set the visibility of the columns in the CheckChanged event of the respective checkboxes.

CS:
 protected void CheckBox1_CheckedChanged(object sender, EventArgs e) 
    { 
        CheckBox chkbx=(CheckBox)sender; 
        RadGrid1.MasterTableView.GetColumn("FirstName").Visible = chkbx.Checked; 
        RadGrid1.Rebind(); 
    } 
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e) 
    { 
         
          CheckBox chkbx=(CheckBox)sender; 
          RadGrid1.MasterTableView.GetColumn("LastName").Visible = chkbx.Checked; 
          RadGrid1.Rebind(); 
    } 


Regards
Shinu.
0
Accepted
Daniel
Telerik team
answered on 30 Oct 2008, 12:27 PM
Hello Brett,

Test the following code:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    GridCommandItem item = e.Item as GridCommandItem; 
 
    if (item != null
    { 
        CheckBoxList list = item.Cells[0].Controls[1] as CheckBoxList; 
        list.DataTextField = "UniqueName"
        list.DataValueField = "Display"
        list.DataSource = RadGrid1.Columns; 
    } 
 
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) 
    CheckBoxList cbList = sender as CheckBoxList; 
    foreach (ListItem listItem in cbList.Items) 
        RadGrid1.MasterTableView.Columns[cbList.Items.IndexOf(listItem)].Display = !listItem.Selected; 

<CommandItemTemplate> 
    <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatDirection="Horizontal" 
        AutoPostBack="True" OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged"
    </asp:CheckBoxList> 
</CommandItemTemplate> 

Additionally I attached a sample project illustrating this approach.

Best regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Mike
Top achievements
Rank 1
Answers by
Baatezu
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
Daniel
Telerik team
Share this question
or