Over a million developers have joined DZone.

Bug in Gridview Editing

·

In This small post is about the bug I found when we do inline editing in GridView control of ASP.Net in web application.

To understand this fully follwing is my gridview code in AspX file

<gridview autogeneratecolumns="false" autogenerateeditbutton="True" gridlines="both" id="grdView" onrowcancelingedit="grdView_RowCancelingEdit" onrowdatabound="grdView_RowDataBound" onrowediting="grdView_RowEditing" onrowupdating="grdView_RowUpdating" runat="server" width="100%">
        <rowstyle cssclass="contentframework-aligncentre">
        <alternatingrowstyle cssclass="contentframework-formdatalabel">
        <headerstyle cssclass="contentframework-dataheadertop">
        <columns>
            <templatefield headerstyle-font-bold="true" headertext="Rate" horizontalalign="Right" itemstyle-="">
                <itemtemplate>
                    <label id="lblRate" runat="server" text="<%#Bind("Currency_Rate") %>"></label>
                </itemtemplate>
                <edititemtemplate>
                    <textbox id="txtRate" maxlength="10" runat="server" text="<%# Bind
                          ("Currency_Rate") %>"></textbox>
                    <requiredfieldvalidator controltovalidate="txtRate" errormessage="*" id="reqRate" runat="server">
                    </requiredfieldvalidator>
                </edititemtemplate>
            </templatefield>
        </columns>
        <emptydatatemplate>
            <label id="lblEmptyRow" runat="server" text="No Data Found"></label>
        </emptydatatemplate>
    </headerstyle></alternatingrowstyle></rowstyle></gridview>

As you see in code I have attahced RowDataBound which fires when data get bound with the each row of the grid and RowEditing which get fire when user press edit button of the row in gridview.

Code in .Aspx.cs File

#region Grid Events
    protected void grdView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdFXRate.EditIndex = e.NewEditIndex;
        BindGrid();
    }

    protected void grdView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdView.EditIndex = -1;
        BindGrid();
    }

    protected void grdView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //get data and update
        int Currency_Rate = 0;
        TextBox txtRate = row.FindControl("txtRate") as TextBox;
        if (txtRate != null)
            Currency_Rate = Convert.ToDouble(txtRate.Text);

        saveDetails();
        grdView.EditIndex = -1;
        BindGrid();
    }

    protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState == DataControlRowState.Edit )
        {
            GridViewRow row = e.Row;
            TextBox txtRate = row.FindControl("txtRate") as TextBox;
            if (txtRate != null)
                txtRate.Attributes.Add("onKeypress", "IntegerAndDecimal(event,'" + txtRate.ClientID + "',true)");
        }
    }
    #endregion Grid Events

As you see in code I have written code RowDataBound which find textbox control and attach javascript with it.  And second thing to notedown is that it finds textbox and attach script with it when row is in edit mode. This code works fine its attach script wwith the textbox when row is in edit mode.
Problem
But this code dont work when I click on edit butoon of alternet row which means that it not satisfy if condition. In Edit mode alternet row sate is "Alternet | Edit" where as when you click edit of non-alternetrow rostate is "Edit" which statify the if condition.

You weill get edit by below image where in immediate window it shows sate of alternet row.

Solution
Following is solution to avoid problem with the alternet row edit.
protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if ((e.Row.RowState & DataControlRowState.Edit) > 0)
     {
          GridViewRow row = e.Row;
          TextBox txtRate = row.FindControl("txtRate") as TextBox;
          if (txtRate != null)
               txtRate.Attributes.Add(
                    "onKeypress", "IntegerAndDecimal(event,'" + txtRate.ClientID + "',true)");
     }
}
As you see in code condtion e.Row.RowState & DataControlRowState.Edit satisfy the for both alternet and non-alternet row and works fine.
Topics:

Published at DZone with permission of Pranay Rana, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}