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

Setting width of textboxes for GridBoundColumn in RadGrid

4 Answers 1221 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carl
Top achievements
Rank 1
Carl asked on 25 Oct 2008, 01:49 AM
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!!!

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 27 Oct 2008, 05:16 AM
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.
0
Carl
Top achievements
Rank 1
answered on 27 Oct 2008, 06:27 PM
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.
0
Nikolay Rusev
Telerik team
answered on 28 Oct 2008, 12:17 PM
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.
0
Graham
Top achievements
Rank 1
answered on 13 Aug 2010, 12:59 PM

 

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

 

 

Tags
Grid
Asked by
Carl
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Carl
Top achievements
Rank 1
Nikolay Rusev
Telerik team
Graham
Top achievements
Rank 1
Share this question
or