Click here to Skip to main content
12,076,727 members (45,766 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked

Bug in Gridview Editing

, 12 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Bug in Gridview Editing

Tip 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 width="100%" runat="server" onrowupdating="grdView_RowUpdating" onrowediting="grdView_RowEditing" onrowdatabound="grdView_RowDataBound" onrowcancelingedit="grdView_RowCancelingEdit" id="grdView" gridlines="both" autogenerateeditbutton="True" autogeneratecolumns="false">
        <rowstyle cssclass="contentframework-aligncentre">
        <alternatingrowstyle cssclass="contentframework-formdatalabel">
        <headerstyle cssclass="contentframework-dataheadertop">
            <templatefield horizontalalign="Right" headertext="Rate" headerstyle-font-bold="true">
                    <label text="&lt;%#Bind(&quot;Currency_Rate&quot;) %&gt;" runat="server" id="lblRate" />
                    <textbox text="&lt;%# Bind
                          (&quot;Currency_Rate&quot;) %&gt;" runat="server" maxlength="10" id="txtRate" />
                    <requiredfieldvalidator runat="server" id="reqRate" errormessage="*" controltovalidate="txtRate">
            <label text="No Data Found" runat="server" id="lblEmptyRow" />

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;
    protected void grdView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        grdView.EditIndex = -1;
    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);
        grdView.EditIndex = -1;
    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.


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.


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)
                    "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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Pranay Rana
Software Developer (Senior) GMind Solusion
India India

Microsoft C# MVP (12-13)

Hey, I am Pranay Rana, working as a ITA in MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog

StackOverFlow -
My CV :-


You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
sandeepkumarvemula12-Feb-13 2:09
membersandeepkumarvemula12-Feb-13 2:09 
GeneralMy vote of 1 Pin
Abdul Quader Mamun13-Aug-12 0:19
memberAbdul Quader Mamun13-Aug-12 0:19 
GeneralRe: My vote of 1 Pin
Abdul Quader Mamun13-Aug-12 0:20
memberAbdul Quader Mamun13-Aug-12 0:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160212.1 | Last Updated 12 Aug 2012
Article Copyright 2012 by Pranay Rana
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid