Click here to Skip to main content
16,004,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP.NET
<asp:UpdatePanel ID="up2" runat="server">
        <contenttemplate>
            <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" 
                AutoGenerateEditButton="true" onrowediting="gv_RowEditing" 
                onrowcancelingedit="gv_RowCancelingEdit" onrowupdating="gv_RowUpdating" DataKeyNames="locationid">
                <columns>
                    <asp:TemplateField HeaderText="LocationID">
                        <itemtemplate>
                            <asp:Label ID="lbllocation" runat="server" Text='<%# Bind("locationid") %>'>
                        </itemtemplate>
                    
                    <asp:TemplateField HeaderText="RegionalGroup">
                        <itemtemplate>
                            <%# Eval("regionalgroup") %>
                        </itemtemplate>
                        <edititemtemplate>
                            <asp:TextBox ID="txtregional" runat="server" Text='<%# Eval("regionalgroup") %>'>
                        </edititemtemplate>
                    
                </columns>
            
            <asp:Label ID="lblmsg" runat="server">
        </contenttemplate>

in CS file
C#
protected void Page_Load(object sender, EventArgs e)
       {
           fillLocation();
       }

       protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
       {
           gv.EditIndex = e.NewEditIndex;
           fillLocation();
       }

       protected void fillLocation()
       {
           Employees emp = new Employees();
           gv.DataSource = emp.GetLocations();
           gv.DataBind();

       }

       protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           int locationid = Int32.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
           int intresult = 0;
           GridViewRow row=gv.Rows[e.RowIndex];

           TextBox tlo = (TextBox)row.FindControl("txtregional");
           Location lo = new Location();
           Employees emp = new Employees();
           try
           {
               lo.locationid = locationid;
               lo.regionalgroup = tlo.Text;
               intresult = emp.updatelocation(lo);
               if (intresult > 0)
               {
                   lblmsg.Text = "Record updated Sucessfully";
               }
               else
               {
                   lblmsg.Text = "Record couldnt updated";
               }

           }

           catch (Exception ex)
           {
               lblmsg.Text = ex.Message;
           }

           finally
           {
               lo = null;
               emp = null;
           }
           gv.EditIndex = -1;
           fillLocation();
       }

       protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
       {
           gv.EditIndex=-1;
           fillLocation();
       }

but it is not getting update is there any problem
please help
thank you.
Posted
Updated 3-Jan-12 21:07pm
v3

try IsPostBack() condition in page load
 
Share this answer
 
Itz quite interesting problem. If you select the GridView in the designer, and select that entry in the Properties box, you can select the correct values. The result should change your source line to look something like the following (in bold):

XML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"  DataKeyNames="id" DataSourceID="SqlDataSource1">


All the tutorials mention that the DataKeyNames entry is automatically added for you. However, I discovered that if you change the SqlDataSource1 control to add or delete columns, a dialog pops asking if you want to "Refresh the Fields and Keys ..." This is where the problem occurs since it clears the DataKeyNames entry. Also, you may not want a DropDownList in the ItemTemplate, but may want to leave it alone as a label and Eval it to the field to which the licensee_id field points.
 
Share this answer
 
v2
Comments
sreekanth12 4-Jan-12 3:44am    
see my question once agan. here i am not using sqldatasource. I want to update programatically.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900