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

How the width of autogenerated column is determind?

5 Answers 196 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ahad
Top achievements
Rank 1
Ahad asked on 03 Jul 2012, 03:25 PM
Hi there,
I am using RadGrid control which is populated with DataSets of different types. I have observed that when database columns are of type decimal, RadGrid makes the header of that column quite wide which doesn't looks pretty.  (all black circle columns are decimal types). As I assign data on run time, I dont want to use fixed header width because no. of return columns are based on database view.
Is there a solution to this problem?
Thanks !

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 04 Jul 2012, 04:47 AM
Hello Ahad,

Try setting the width in ColumnCreated event as shown below.
C#:
protected void RadGrid1_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e)
{
 if (e.Column.UniqueName == "UniqueName")
 {
   e.Column.FilterControlWidth = Unit.Pixel(20);
 }
}

Thanks,
Shinu.
0
Ahad
Top achievements
Rank 1
answered on 04 Jul 2012, 08:07 AM
Hi Shinu,
Thanks for your reply but this doesn't help me at all. As I mentioned before, I dont know the column names at compile time. User selects a database view and that view is assigned to Grid at run time.

My question was how Grid decides by itself that it should allot so much space for decimal / double types? If I return all string types value, space allocation is fine.

I think there is some internal mechanism that decides about it e.g. if column names in Database view are written with CamelCase like ProjectName, Grid always makes a sapce in between Project and Name and displays the column header as "Project Name".

Any idea?

Regards,
0
Shinu
Top achievements
Rank 2
answered on 05 Jul 2012, 04:51 AM
Hello Ahad,

In case you do not know the UniqueNames of the columns, you could loop through the AutoGeneratedColumns collection of the current GridTableView and pick the UniqueName of each column fulfilling a given condition and use it to access a cell in the current row.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
     GridDataItem item = e.Item as GridDataItem;
     foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
     {
         if (col.DataType == typeof(decimal))
          {
              item[col.UniqueName].Width = Unit.Pixel(10);
           }
       }
     }
}
Also check the following help documentation which explains the same.
Working with Auto-Generated Columns

Thanks,
Shinu.
0
Ahad
Top achievements
Rank 1
answered on 13 Jul 2012, 12:40 PM
Hi Shinu,
I stil not able to reduce the size. I am using the following code and it seems to have no effect on Grid.
I am also attaching screenshot from my GUI. Here following columns are either numeric "Min SL", TargetSL, Current Month..


Below are methods that I am using...


<telerik:RadGrid ID="RadGrid" OnPreRender="RadGrid_PreRender" OnSortCommand="RadGrid_SortCommand" OnPageIndexChanged="RadGrid_PageIndexChanged"
                Width="100%" OnPageSizeChanged="RadGrid_PageSizeChanged" OnItemCommand="RadGrid_ItemCommand" HeaderStyle-Width="8%"
                    AllowSorting="True" PageSize="15" AllowPaging="True" OnColumnCreated="RadGrid_ColumnCreated" OnItemDataBound="RadGrid_ItemDataBound"
                AllowMultiRowSelection="True" runat="server" Gridlines="None" ClientSettings-Resizing-AllowResizeToFit="False"
            Skin="Sunset" AllowFilteringByColumn="True" CellSpacing="0" ShowFooter="True" ShowStatusBar="True" BorderStyle="None" Height="95%">
            <GroupingSettings CaseSensitive="false" />
            <ExportSettings HideStructureColumns="true" ExportOnlyData="true" IgnorePaging="true" />
            <MasterTableView Width="100%" CommandItemDisplay="Top">  
            <CommandItemSettings ShowExportToWordButton="true" ShowExportToExcelButton="true"
                ShowExportToCsvButton="true"  ShowAddNewRecordButton="false"/>
        </MasterTableView>
            <PagerStyle Mode="NextPrevAndNumeric" />
              
    </telerik:RadGrid>


protected void RadGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = e.Item as GridDataItem;
            foreach (GridColumn col in RadGrid.MasterTableView.AutoGeneratedColumns)
            {
                if (col.DataType == typeof(decimal) | col.DataType == typeof(Int64))
                    item[col.UniqueName].Width = Unit.Pixel(10);
            }
        }
    }


protected void RadGrid_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
   {
 
       if (e.Column is GridBoundColumn)
       {
           (e.Column as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>";
 
           if (e.Column.DataType == typeof(DateTime))
               (e.Column as GridBoundColumn).DataFormatString = "{0:dd/MM/yyyy}";
 
           if (e.Column is GridNumericColumn)
               e.Column.ItemStyle.Width = Unit.Pixel(10);
       }
   }

    }
0
moncibubu
Top achievements
Rank 1
answered on 13 Jul 2012, 12:55 PM
Hello,

please try to change Radgrid width from 100% to fixed width (e.g. 800px), and delete width declaration from MasterTableView section. Then Radgrid should better calculate and resize columns width...

Best regards

moncibubu
Tags
Grid
Asked by
Ahad
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ahad
Top achievements
Rank 1
moncibubu
Top achievements
Rank 1
Share this question
or