locked
how to update a gridview to strip white spaces from a textbox? RRS feed

  • Question

  • User-1651858287 posted

    I am trying to update a textbox in a gridveiw control to remove all white spaces

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex != e.Row.RowIndex)
    {

    TextBox Tstr = (TextBox)e.Row.FindControl("TextBox1");
    if ( !string.IsNullOrEmpty(Tstr.Text))
    {
    string str = Tstr.Text;
    str = str.Replace(" ", String.Empty);
    Tstr.Text = str;
    }

    }
    }

    Tuesday, October 31, 2017 2:34 PM

All replies

  • User2103319870 posted

    I just tried your code and its working fine. Could you please provide some details about the issue which you are facing

    Tuesday, October 31, 2017 3:02 PM
  • User-1651858287 posted

    I am  geting 

    if(!string.IsNullOrEmpty(Tstr.Text))

    System.NullReferenceException was unhandled by user code

    Tuesday, October 31, 2017 3:47 PM
  • User2103319870 posted

    f(!string.IsNullOrEmpty(Tstr.Text))

    System.NullReferenceException was unhandled by user code

    Please post your gridview HTML code

    Tuesday, October 31, 2017 3:52 PM
  • User2049726087 posted
    Check whether there is a text box with name TextBox1 in your grid view, inside template field.
    Tuesday, October 31, 2017 4:48 PM
  • User-1651858287 posted

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" DataKeyNames="Id" DataSourceID="SqlDataSource1" ForeColor="Black" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
    <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" Visible="False" />
    <asp:TemplateField HeaderText="Period" SortExpression="Period">
    <EditItemTemplate>
    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Period") %>'></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="Update" ></asp:RequiredFieldValidator>
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Period") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField ShowHeader="False">
    <EditItemTemplate>
    <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" ValidationGroup="Update" />
    &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
    <ItemTemplate>
    <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
    &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
    <RowStyle BackColor="White" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#808080" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#383838" />
    </asp:GridView>

    Tuesday, October 31, 2017 5:28 PM
  • User2103319870 posted

    if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex != e.Row.RowIndex)

    You have a wrong condition to check if the Gridview is in EditMode or not. Due to this issue code will try to find the control when gridview is readonly mode which results in null reference exception. Change the condition like below

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
            {
    
                if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) > 0))
                {
                    TextBox Tstr = (TextBox)e.Row.FindControl("TextBox1");
                    if (!string.IsNullOrEmpty(Tstr.Text))
                    {
                        string str = Tstr.Text;
                        str = str.Replace(" ", String.Empty);
                        Tstr.Text = str;
                    }
                }
           }

    Tuesday, October 31, 2017 7:31 PM
  • User-707554951 posted

    Hi  sweetSteal,

    Complete sample as below:

       <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" 
                    AutoGenerateColumns="False" BackColor="#CCCCCC" 
                    BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
                     CellSpacing="2" DataKeyNames="Id" DataSourceID="SqlDataSource1" ForeColor="Black" 
                     OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing">
                    <Columns>
                        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" Visible="False" />
                        <asp:TemplateField HeaderText="Period" SortExpression="Period">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ItemName") %>'></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="TextBox1" ValidationGroup="Update"></asp:RequiredFieldValidator>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("ItemName") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField ShowHeader="False">
                            <EditItemTemplate>
                                <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Update" Text="Update" ValidationGroup="Update" />
                                &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
                                &nbsp;<asp:Button ID="Button2" runat="server" CausesValidation="False" CommandName="Delete" Text="Delete" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                    <FooterStyle BackColor="#CCCCCC" />
                    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                    <RowStyle BackColor="White" />
                    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#808080" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#383838" />
                </asp:GridView>
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                    SelectCommand="SELECT [Id], [Item_Code], [ItemName], [Rate ] AS Rate_ FROM 
    "></asp:SqlDataSource>

    CodeBehidn:

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow && ((e.Row.RowState & DataControlRowState.Edit) > 0))
                {
                    TextBox Tstr = (TextBox)e.Row.FindControl("TextBox1");
                    if (!string.IsNullOrEmpty(Tstr.Text))
                    {
                        string str = Tstr.Text;
                        str = str.Replace(" ", String.Empty);
                        Tstr.Text = str;
                    }
                }
    
    
            }
    
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
                GridView1.EditIndex = e.NewEditIndex;
            }

    Output:

    Best Regards

    Cathy

    Wednesday, November 1, 2017 2:25 AM