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

Conditionally Hide Grid Columns Based Off Checkboxlist Selection

1 Answer 82 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Johnathan
Top achievements
Rank 1
Johnathan asked on 17 Jan 2018, 09:30 PM

I have been utilizing the asp:gridview and use this syntax to determine which columns to hide:

    private int GetColumnIndex(GridView grid, string ColName)
    {
        foreach (DataControlField col in grid.Columns)
        {
            if (col.HeaderText.ToLower().Trim() == ColName.ToLower().Trim())
            {
                return grid.Columns.IndexOf(col);
            }
        }
        return -1;
    }

 

Now when converting to radgrid I get the error of

'Unable to cast object of type 'Telerik.Web.UI.GridBoundColumn' to type 'System.Web.UI.WebControls.DataControlField'.'

With the below syntax:

    private int GetColumnIndexRad(Telerik.Web.UI.RadGrid grid, string colName)
    {
        foreach (DataControlField col in grid.Columns)
        {
            if (col.HeaderText.ToLower().Trim() == colName.ToLower().Trim())
            {
                return grid.Columns.IndexOf(col);
            }
        }
        return -1;
    }

 

What would be the proper way to conditionally hide columns based off a check box list selection?

 

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Attila Antal
Telerik team
answered on 19 Jan 2018, 02:08 PM
Hi Johnathan,

To access the columns of RadGrid, try casting the object to GridColumn instead of the DataControlField.
private int GetColumnIndex(RadGrid grid, string ColName)
{
    foreach (GridColumn col in grid.Columns)
    {
        if (col.HeaderText.ToLower().Trim() == ColName.ToLower().Trim())
        {
            return grid.Columns.IndexOf(col);
        }
    }
    return -1;
}

For more information, you can check out the following articles:

Hope this will prove helpful.


Kind regards,
Attila Antal
Progress Telerik
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Johnathan
Top achievements
Rank 1
Answers by
Attila Antal
Telerik team
Share this question
or