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

Telerik UI ASP.net RADGRID set Columns width programmatically

4 Answers 161 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 26 Feb 2014, 02:34 PM
Hi

I am trying to display a Telerik RadGrid in my project.
I have a problem with changing the column width.
The data source of my grid is a list of objects.
When I add filters to the grid, the columns' width is fixed and I cannot change it / resize it.

P.S. I want to solve this programmatically(in vb code) 

my vb code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
 setColumnsOnGrid(Of object)(lst, RadGrid1, ArrayNameFilds:={"column1", "column2", "column3 ", "etc."})
        End If
    End Sub
     
     
    Private Sub setColumnsOnGrid(Of T)(ByVal lst As List(Of T), ByVal grdName As RadGrid, ByVal ArrayNameFilds As Array)
        Dim nameFiled As String
        grdName.DataSource = lst
        grdName.AllowMultiRowSelection = True
        grdName.MasterTableView.AutoGenerateColumns = False
   
        Dim boundColumn As GridBoundColumn
 
        For i As Integer = 0 To ArrayNameFilds.Length - 1
            nameFiled = ArrayNameFilds(i).ToString()
            boundColumn = New GridBoundColumn()
            grdName.MasterTableView.Columns.Add(boundColumn)
            boundColumn.DataField = nameFiled
            boundColumn.HeaderText = nameFiled
         Next
    End Sub

my aspx:
<telerik:RadGrid ID="grd_test" runat="server" AllowFilteringByColumn="True" AllowPaging="True" AllowSorting="True" CellSpacing="0" GridLines="None">
        <ClientSettings>
            <Scrolling AllowScroll="True" UseStaticHeaders="True" />
        </ClientSettings>
</telerik:RadGrid>

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 27 Feb 2014, 04:20 AM
Hi Daniel,

Please try the following code snippet to set the column width.

VB:
Protected Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
  'Set width for a Column
  Dim gridCol As GridColumn = RadGrid1.MasterTableView.GetColumn("ColumnUniqueName")
  gridCol.HeaderStyle.Width = Unit.Pixel(500)
  'Set width for all columns
  For Each col As GridColumn In RadGrid1.MasterTableView.Columns
      col.HeaderStyle.Width = Unit.Pixel(200)
  Next
End Sub

Thanks,
Princy
0
Daniel
Top achievements
Rank 1
answered on 27 Feb 2014, 10:05 AM
Thanks!! it worked!

but I also have to added the width for the filter:
grdName.MasterTableView.Columns(i).HeaderStyle.Width = System.Web.UI.WebControls.Unit.Pixel(50)
grdName.MasterTableView.Columns(i).FilterControlWidth = System.Web.UI.WebControls.Unit.Pixel(50)

0
Princy
Top achievements
Rank 1
answered on 28 Feb 2014, 03:11 AM
Hi Daniel,

You can set the FilterControlWidth of a column in the ItemCreated event of the RadGrid. Take a look at this article on Setting Filter Textbox Dimensions. Please try and let me know if any concerns.

Thanks,
Princy
0
Nancy Moore
Top achievements
Rank 1
answered on 01 Oct 2015, 08:30 PM
I just wanted to thank you for your post on how to programmatically change the width(s) of radgrid columns in vb code behind.  I read a lot of posts that seemed just too complicated and were not at all what I needed.  Yours was to the point and works like a charm.  Thanks for making my day!
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Daniel
Top achievements
Rank 1
Nancy Moore
Top achievements
Rank 1
Share this question
or