Telerik UI ASP.net RADGRID set Columns width programmatically

5 posts, 0 answers
  1. Daniel
    Daniel avatar
    159 posts
    Member since:
    Feb 2014

    Posted 26 Feb 2014 Link to this post

    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>
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 26 Feb 2014 in reply to Daniel Link to this post

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

    Posted 27 Feb 2014 in reply to Princy Link to this post

    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)

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

    Posted 27 Feb 2014 in reply to Daniel Link to this post

    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
  6. Nancy Moore
    Nancy Moore avatar
    2 posts
    Member since:
    Aug 2009

    Posted 01 Oct 2015 in reply to Princy Link to this post

    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!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017