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

How to set column width in Code behind for DetailTable

3 Answers 529 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kellie
Top achievements
Rank 1
Kellie asked on 25 Nov 2010, 12:29 AM

Problem 1:
I have found a link on how to adjust the width of declaritive columns through code behind:
http://www.telerik.com/help/aspnet-ajax/grdusingcolumns.html

protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
 foreach(GridColumn column in RadGrid1.Columns)
 {
   if (column.UniqueName == "BirthDate")
   {
     (column as GridBoundColumn).ReadOnly = true;
     (column as GridBoundColumn).DataFormatString = "{0:D}";
     break;
   }
 }
 RadGrid1.Rebind();
}  

Although, when I use rebind() as in the example, the detailtable expand button stops working in my grid. When it is clicked the grid rebinds again and the rows never expand.
How do I get the Expand to work after implementing the above code?

Problem 2: How do I set the width of the columns in the code behind for the Detailtable? Can you please provide a simple example. Is it done in the PreRender event? Will I need to rebind the Detailtable as well?

Thanks so much for your time,
Kellie

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 25 Nov 2010, 06:46 AM
Hello Kellie,

 The Rebind() in your code will execute on every postback (includes when performing Expand/Collapse). When Rebind() is invoked all the expanded items will be collapsed and thats why the problem occurs. Please try the same code without using Rebind()  And you can set the width of the detail table like below.  

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
   {
       foreach (GridColumn column in RadGrid1.Columns)
       {
           if (column.UniqueName == "FirstName")
           {
               (column as GridBoundColumn).ReadOnly = true;
               (column as GridBoundColumn).DataFormatString = "{0:D}";
               break;
           }
       }
       RadGrid1.MasterTableView.DetailTables[0].GetColumn("FirstName").HeaderStyle.Width=Unit.Pixel(100);
   }

Thanks,
Princy.
0
Kellie
Top achievements
Rank 1
answered on 25 Nov 2010, 09:36 PM
Thank you.
I am a little confused why a Rebind() is suggested in the example (and in some forum posts), but you tell me I don't have to use it...

This is what I ended up doing.

protected void RadGridRoadRoadwaySegment_PreRender(object sender, System.EventArgs e)
{
    var currentGrid = (RadGrid)sender;
    SetWidth(currentGrid);
}


private void SetWidth(RadGrid myGrid)
{
    //Set width of MasterTable
    var myMasterTableColumns = myGrid.MasterTableView.Columns;
    foreach (GridColumn column in myMasterTableColumns)
    {
        SetMasterColumnWidth(column);
    }
    //Set width of DetailTables (if any)
    if (myGrid.MasterTableView.HasDetailTables)
    {
        foreach (var detailTable in myGrid.MasterTableView.DetailTables)
        {
            var myDetailTableColumns = detailTable.Columns;
            foreach (GridColumn column in myDetailTableColumns)
            {
                SetDetailColumnWidth(column);
            }
        }
    }
}
0
Pavlina
Telerik team
answered on 29 Nov 2010, 04:08 PM
Hello Kellie,

It depends on your specific scenario if its needed to call Rebind() method or not. For example to change the Visible property for the columns or put the item in edit mode you should call Rebind() method to reflect the changes. Calling Rebind() is not needed if you want to change the columns width or select some column.

Additionally, you can refer to the following code library which illustrates how to retain the expanded/selected state for grid items in hierarchy upon a rebind command.
Retain expanded/selected state in hierarchy on rebind

I hope this information helps.

All the best,
Pavlina
the Telerik team
Browse the vast support resources we have to jumpstart your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Tags
Grid
Asked by
Kellie
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kellie
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or