How to set column width in Code behind for DetailTable

4 posts, 0 answers
  1. Kellie
    Kellie avatar
    29 posts
    Member since:
    Feb 2009

    Posted 24 Nov 2010 Link to this post

    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

  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Nov 2010 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kellie
    Kellie avatar
    29 posts
    Member since:
    Feb 2009

    Posted 25 Nov 2010 Link to this post

    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);
                }
            }
        }
    }
  5. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 29 Nov 2010 Link to this post

    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.
Back to Top