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

grid.MasterTableView.Columns is empty

2 Answers 222 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 10 Jan 2013, 01:31 AM
I have a RadGrid where I am auto generating columns because they can changed depending on the source. And I want to display only certain columns and in a specific order. Its easiest for me to store the columns to display in a comma delimited string. I created the code below to try to do this. However it fails because in the Page_Load method the grid.MasterTableView.Columns is always empty, even though I do a DataBind first. Any idea how I can get this to work?

Thanks,
Jason

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // bind
        grid.MasterTableView.DataBind();
        grid.DataBind();
  
        // columns
        var cols = grid.MasterTableView.Columns;
        foreach (GridColumn c in cols)
        {
            c.Visible = false;
        }
        // fieldNames is a comma delimited list of column names in the correct order
        var fields = fieldNames.Split(new char[',']);
        for (var j = 0; j < fields.Length; j++)
        {
            var c = cols.FindByUniqueNameSafe(fields[j]);
            if (c != null)
            {
                c.Visible = true;
                c.OrderIndex = j;
            }
        }
    }
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Jayesh Goyani
Top achievements
Rank 2
answered on 10 Jan 2013, 05:12 AM
Hello,

Access Autogenerated by using below code snippet.
RadGrid1.MasterTableView.AutoGeneratedColumns

You can only access this column in/after columnCreated, ItemCreated and prerender event.

http://www.telerik.com/help/aspnet-ajax/grid-control-lifecycle.html
http://www.telerik.com/help/aspnet-ajax/grid-event-sequence.html

Thanks,
Jayesh Goyani
0
Jason
Top achievements
Rank 1
answered on 10 Jan 2013, 10:19 PM
Thank you Jayesh. That got it working. And the event and life cycle links are very helpful. Thank you.

For anyone with the same issue, below is the code that is working to reorder and remove columns.

protected void grid_PreRender(object sender, EventArgs e)
{
    // columns
    var cols = grid.MasterTableView.AutoGeneratedColumns;
    foreach (GridColumn c in cols)
    {
        c.Visible = false;
    }
    // fieldNames is a comma delimited list of column names in the correct order
    var fields = fieldNames.Split(new char[',']);
    for (var j = 0; j < fields.Length; j++)
    {
        GridColumn col = null;
        foreach (GridColumn c in cols)
        {
            if (c.UniqueName == fields[j])
            {
                col = c;
                break;
            }
        }
        if (col != null)
        {
            col.Visible = true;
            col.OrderIndex = j;
        }
    }
 
    // rebind
    grid.MasterTableView.Rebind();
}
Tags
Grid
Asked by
Jason
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Jason
Top achievements
Rank 1
Share this question
or