Setting width of textboxes for GridBoundColumn in RadGrid

5 posts, 0 answers
  1. Carl
    Carl avatar
    195 posts
    Member since:
    Oct 2012

    Posted 24 Oct 2008 Link to this post

    I have not found any simple declarative property that enables me to set the width of textboxes for editable GridBoundColumns in RadGrid.I tried ItemStyle-Width but that does not seem to work. Setting MaxLength does not alter the visual display size of the TextBox control.

    So how to do it ? in a simple declarative way for textboxes when using EditMode="EditForms"It seems that this is an important feature that should be easy to set declaratively in the *.aspx file....

    If it's not possible, then I hope the next release will add this important feature!!!
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 27 Oct 2008 Link to this post

    Hi Carl,

    You can set the width of the textbox of a Grid bound column in edit mode in the code behind as shown below.

    CS:
      protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) 
        { 
            if ((e.Item is GridEditFormItem) && (e.Item.IsInEditMode)) 
            { 
                GridEditFormItem editItem = (GridEditFormItem)e.Item; 
                TextBox txtbx = (TextBox)editItem["columnUniqueName"].Controls[0]; 
                txtbx.Width = Unit.Pixel(150); 
            } 
        } 

    Other suggestion will be to use column editors so that you can set its width declaratively also.
    Custom editors extending auto-generated editors
    Declarative style editors

    Thanks
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Carl
    Carl avatar
    195 posts
    Member since:
    Oct 2012

    Posted 27 Oct 2008 Link to this post

    Thank you for the pointers. I do have it working now.

    But I want to suggest again that a new feature be added to the next release. It just should not take so many extra steps for the developer to write code just to adjust the size of a textbox in a datagrid.

    telerik:GridBoundColumn does have a declarative attribute for MaxLength so it should also have a declarative attribute for Columns (that corresponds to a TextBox.Columns) so that it is just as easy to set the display number of characters as it is to set the maxlength number of characters for a datafield shown as a textbox in the datagrid.

    I hope that this feature can be added to the next release.
  5. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 28 Oct 2008 Link to this post

    Hello Carl,

    Currently we do not plan to expose such a property. However, you can set the style of column editor in the markup as shown below:

    Sample RadGrid definition
    <telerik:RadGrid runat="server" ID="RadGrid1"   
                AutoGenerateColumns="false"   
                OnNeedDataSource="RadGrid1_NeedDataSource" AutoGenerateEditColumn="true">  
                <MasterTableView DataKeyNames="ID">  
                    <Columns> 
                        <telerik:GridBoundColumn DataField="ID" HeaderText="ID">  
                        </telerik:GridBoundColumn>                      
                        <telerik:GridBoundColumn DataField="Text"   
                            HeaderText="Text" ColumnEditorID="MyEditor">  
                        </telerik:GridBoundColumn>                                          
                    </Columns> 
                </MasterTableView> 
            </telerik:RadGrid> 

    RadGrid declarative textbox style editor
    <telerik:GridTextBoxColumnEditor runat="server" ID="MyEditor">  
      <TextBoxStyle Width="50px" /> 
    </telerik:GridTextBoxColumnEditor> 

    Best regards,
    Nikolay
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. Graham
    Graham avatar
    4 posts
    Member since:
    Jul 2010

    Posted 13 Aug 2010 Link to this post

     

    Protected Sub RadGrid1_PreRender1(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender

     

    What I did was set the maxlength for every textbox in the Rad Grid.  You can then loop through each textbox and set the size * 7 which works out to the correct size in pixels.

     


        Protected Sub RadGrid1_PreRender1(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadGrid1.PreRender
            For Each edititem As GridEditFormItem In RadGrid1.MasterTableView.GetItems(GridItemType.EditFormItem)
                For Each col As GridColumn In RadGrid1.MasterTableView.RenderColumns
                    If col.ColumnType = "GridBoundColumn" Then
                        If edititem.IsInEditMode Then
                            Dim txtbx As TextBox = DirectCast(edititem(col.UniqueName).Controls(0), TextBox)

                            If Val(txtbx.MaxLength) > 1 Then
                                Dim iWidth As Integer = txtbx.MaxLength * 7
                                If iWidth > 1000 Then
                                    iWidth = 1000
                                End If
                                txtbx.Width = iWidth
                            End If

                        End If
                    End If
                Next
            Next
        End Sub



    Hope this helps.
    Graham
    http://www.northerncs.com

     

     

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017