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

Last Reply 17 days ago By Andrea

Posted 17 days 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">
                <Columns>
                    <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">
                        <EditItemTemplate>
                            <asp:TextBox ID="TxtMark" runat="server" Text='<%# Bind("Mark") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("Mark") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

 

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

 

        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]);
            MarkValue=Convert.ToDouble(Mark);
            SqlDataSource1.Update();
        }
        protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
        {
            try
            {
                e.Command.Parameters["@Mark"].Value = MarkValue;
                e.Command.Parameters["@id"].Value = Id;
            }
            catch
            {

            }
        }

how can I fix this problem?

Posted 17 days ago
Hi @dorsa,
Please try the following

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

It might help you.

Cheers Andrea.