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

Hide columns after data-binding

5 Answers 433 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ed
Top achievements
Rank 1
Ed asked on 19 Nov 2012, 06:56 PM
I have a simple RadGrid:

<telerik:RadGrid ID="rgHeaderKey" runat="server">
  <HeaderStyle Font-Bold="true" />
  <MasterTableView HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="true" />
</telerik:RadGrid>

The columns and rows are bound to a table in a database; yet some of the columns do not contain any data (that is, all the entries in that column are NULL).  I need to hide those columns that do not contain any data after the grid has been filled.  I tried the following handler for the OnDataBound event:

protected void GridHeaderKeyDataBound(object sender, EventArgs e)
{
    // after data-binding, hide any columns that have no entries
    for(int i = 0; i < rgHeaderKey.Columns.Count; i++)
    {
        // check the entry in the first row
        GridColumn col = rgHeaderKey.Columns[i];
        if (String.IsNullOrEmpty(rgHeaderKey.Items[0][col].Text)) col.Display = false;
    }
}

This fails, however, because rgHeaderKey.Columns.Count is always 0, even after the data have been bound (which I find a bit strange).  I realise this might be because the columns are automatically generated: however, I need it this way because I do not know the names of the columns in advance.

Any help would be very much appreciated.

5 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 Nov 2012, 04:53 AM
Hi,

Try the following code snippet to get the column count.

C#:
for (int i = 0; i < rgHeaderKey.MasterTableView.RenderColumns.Length; i++)
{
     //your code
}

Thanks,
Shinu.
0
Ed
Top achievements
Rank 1
answered on 20 Nov 2012, 11:44 AM
Excellent, Shinu -- that did the trick.  Thanks very much.
0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 05 Mar 2014, 05:18 PM
I have the same need and tried the above, but do not find how to hide a specific columns
0
Shinu
Top achievements
Rank 2
answered on 06 Mar 2014, 06:03 AM
Hi,

If you want to check a specific column Text and hide that column, please try the following code snippet.

C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
   GridDataItem items = (GridDataItem)e.Item;
   if (items["ColumnUniqueName"].Text == " ")
   {
     RadGrid1.MasterTableView.GetColumn("ColumnUniqueName").Display = false;
   }
  }    
}

Thanks,
Shinu
0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 06 Mar 2014, 06:52 AM
Ok thanks a lot, I missed the "MasterTableView" part of it
Tags
Grid
Asked by
Ed
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ed
Top achievements
Rank 1
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Share this question
or