[Solved] GridView row update always first row data in ASP.Net

Last Reply 7 months ago By Andrea

Posted 7 months ago

I have a grid view, I want to edit the data of my grid. but when I want to update it just update the first row.

for example, if I want to update the second row it also updates the first row.

this is my code: 

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="455px"
                AutoGenerateEditButton="True" OnRowUpdating="GridView1_RowUpdating" DataKeyNames="PersonId">
                    <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                    <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                    <asp:BoundField DataField="PersonId" HeaderText="PersonId" SortExpression="PersonId" />
                    <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                    <asp:BoundField DataField="code" HeaderText="code" SortExpression="code" />
                    <asp:TemplateField HeaderText="Mark" SortExpression="Mark">
                            <asp:TextBox ID="TxtMark" runat="server" Text='<%# Bind("Mark") %>'></asp:TextBox>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Mark") %>'></asp:Label>


            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                SelectCommand="myproc" SelectCommandType="StoredProcedure"
                UpdateCommand="UPDATE EDU_Lesson2 SET Mark=@Mark WHERE Id=@Id"
                    <asp:Parameter DefaultValue="1" Name="id" Type="Int32" />
                    <asp:Parameter Name="Mark" Type="Double" />
                    <asp:Parameter Name="id" Type="Int32" />
                    <%--<asp:ControlParameter ControlID="TxtMark" Name="Mark" PropertyName="text" Type="Double" />--%>


        string Mark;
        int Id;
        Double MarkValue;
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            GridViewRow row = GridView1.Rows[e.RowIndex];
            Mark = (row.FindControl("TxtMark") as TextBox).Text;
            Id =Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
        protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
                e.Command.Parameters["@Mark"].Value = MarkValue;
                e.Command.Parameters["@id"].Value = Id;


how can I fix this problem?

Posted 7 months ago
Hi @dorsa,
Please try the following

Always first row data getting updated in ASP.Net GridView update

It might help you.

Cheers Andrea.