locked
Gridview Boundfield maxlength RRS feed

  • Question

  • Hi Guys,

    Can you show me sample code on how to set boundfield of SPGridview on edit its textbox maxLength and enter integer only?

    I'm using a gridview in my web part and need to set maxlength. Thanks.


    ®©™½¼


    • Edited by jwill92 Thursday, February 16, 2012 1:54 PM
    Thursday, February 16, 2012 1:45 PM

Answers

  • Hi jwill92,

    Here is an article about how to add TemplateField column programmatically:
    http://suehernandez.wordpress.com/2009/07/07/moss-spgridview-with-checkbox-column/

    Thanks,
    Lhan Han

    • Marked as answer by Lhan Han Monday, February 27, 2012 1:49 AM
    Wednesday, February 22, 2012 3:54 AM
  • Thanks Lhan,  I found a solution using the RowDataBound

    Private Sub _grid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    
       For Each r As GridViewRow In _grid.Rows
                If (r.RowState = DataControlRowState.Edit) OrElse (r.RowState = (DataControlRowState.Alternate Or DataControlRowState.Edit)) Then
                 Dim box as Textbox = TryCast(r.Cells(7).Controls(0), TextBox)
                box.MaxLength = 11
             
                End If
            Next
            
       
        End Sub


    ®©™½¼

    • Marked as answer by Lhan Han Monday, February 27, 2012 1:49 AM
    Thursday, February 23, 2012 6:53 AM

All replies

  • Hi jwill92

    You can use the asp:TemplateField to achieve your requirement. Following is a sample code:

    <asp:TemplateField HeaderText="Username">

    <ItemTemplate>

    <asp:Label ID="Label1" Text='<%# Eval("Username")%>' runat="server" Font-Names="Verdana"></asp:Label>

    < /ItemTemplate>

    <EditItemTemplate>

    <asp:RequiredFieldValidator ErrorMessage="This field cannot be blank" ControlToValidate="TextBox1" ID="RequiredFieldValidator1" runat="server"></asp:RequiredFieldValidator>

    <asp:TextBox ID="TextBox1" MaxLength="20"
    Width="98%" Text='<%# Eval("Username")%>' runat="server"></asp:TextBox>

    < /EditItemTemplate>

    < /asp:TemplateField>

    For more information, please refer to the following link:

    http://www.reversealchemy.net/blog/2009/07/08/building-a-spgridview-control-intermezzo-templatefields-and-the-rowcommand-event/

    http://forums.asp.net/t/982556.aspx/1

    Thanks,

    Lhan Han


    • Edited by Lhan Han Sunday, February 19, 2012 1:55 PM
    Sunday, February 19, 2012 1:54 PM
  • Thanks Han,  But im doing the gridview programmatically in the CreateChildControl() of my web part. Any idea on how to set it programmatically?


    ®©™½¼

    Monday, February 20, 2012 6:26 AM
  • Hi jwill92,

    Here is an article about how to add TemplateField column programmatically:
    http://suehernandez.wordpress.com/2009/07/07/moss-spgridview-with-checkbox-column/

    Thanks,
    Lhan Han

    • Marked as answer by Lhan Han Monday, February 27, 2012 1:49 AM
    Wednesday, February 22, 2012 3:54 AM
  • Thanks Lhan,  I found a solution using the RowDataBound

    Private Sub _grid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    
       For Each r As GridViewRow In _grid.Rows
                If (r.RowState = DataControlRowState.Edit) OrElse (r.RowState = (DataControlRowState.Alternate Or DataControlRowState.Edit)) Then
                 Dim box as Textbox = TryCast(r.Cells(7).Controls(0), TextBox)
                box.MaxLength = 11
             
                End If
            Next
            
       
        End Sub


    ®©™½¼

    • Marked as answer by Lhan Han Monday, February 27, 2012 1:49 AM
    Thursday, February 23, 2012 6:53 AM