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

Remove column from Radgrid

6 Answers 1098 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Shyam
Top achievements
Rank 1
Shyam asked on 30 Dec 2011, 02:27 PM
How to remove dynamically created column from Radgrid.

6 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Dec 2011, 02:55 PM
Hello,

protected void RadGrid1_PreRender(object sender, EventArgs e)
  {
     foreach(GridColumn column in  RadGrid1.Columns)
     {
         if (column.UniqueName == "YourColumnUniqueNane")
         {
             column.Visible = false;
         }
     }
  }


Thanks,
Jayesh Goyani
0
Shyam
Top achievements
Rank 1
answered on 30 Dec 2011, 03:00 PM
Thanks Jayesh.

Can we hide column depending upon data. If I don't have data then just empty column is displayed so how do I hide it.
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 30 Dec 2011, 05:18 PM
Hello,

List<GridDataItem> items = (from item in RadGrid1.MasterTableView.Items.Cast<GridDataItem>()
                                        where string.IsNullOrEmpty(item["ColumnUniqueName"].Text)
                                        select item).ToList();
 
            if (items != null && items.Count > 0)
            {
                foreach (GridColumn column in RadGrid1.Columns)
                {
                    if (column.UniqueName == "YourColumnUniqueNane")
                    {
                        column.Visible = false;
                    }
                }
            }


Thanks,
Jayesh Goyani
0
Shyam
Top achievements
Rank 1
answered on 04 Jan 2012, 10:12 AM
Thanks Again. :)  I think it will work for me.
One more question  Columns.Remove() or  Columns.RemoveAt()   will do my work or not. I have tried both the methods but not found any luck.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 04 Jan 2012, 12:12 PM
Hello,

If you need to remove a column from a Telerik RadGrid at runtime, subscribe to the grid's DataBound event, find the column by its unique name, and set its display property to false.  This is the correct way to do this without breaking the filter functionality.  For a long time I was subscribing to the grid's ItemDataBound event and hiding each dataitem for the column- only now did I realize that the filter was broken by doing it that way.

 

Example:

 

    protected void rgPartDispositionHistory_DataBound(object sender, EventArgs e)
    {
        rgPartDispositionHistory.MasterTableView.Columns.FindByUniqueName("PeopleSoftId").Display = true;
    }



Thanks,
Jayesh Goyani
0
Shyam
Top achievements
Rank 1
answered on 04 Jan 2012, 01:12 PM
I really appreciate your help. Your solution gives me direction to solve my problem.
Tags
Grid
Asked by
Shyam
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shyam
Top achievements
Rank 1
Share this question
or