tblSetMarks: Class,Section, AdmissionNo,SubjectID,SubjectCode,MaxMarks,Marks
I want to update marks inside gridview using edit command button. when I update the record It performs updation on the above row instead of desired row.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Class="table table-striped table-bordered table-hover"
OnDataBound="OnDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField ItemStyle-Width="20">
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ClassName" HeaderText="Class" ItemStyle-Width="100" />
<asp:BoundField DataField="SectionName" HeaderText="Section" ItemStyle-Width="100" />
<asp:TemplateField HeaderText="Reg No.">
<ItemTemplate>
<asp:Label ID="lbl_AdNo" runat="server" Text='<%# Eval("AdmissionNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SName" HeaderText="Student Name" ItemStyle-Width="100" />
<asp:BoundField DataField="FName" HeaderText="Father Name" ItemStyle-Width="100" />
<asp:TemplateField HeaderText="Subject Code">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("SubjectID") %>' Width="75"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="SubjectName" HeaderText="Subject Name" ItemStyle-Width="100" />
<asp:BoundField DataField="MaxMarks" HeaderText="Total Marks" ItemStyle-Width="100" />
<asp:BoundField DataField="PassMarks" HeaderText="Pass Marks" ItemStyle-Width="100" />
<asp:TemplateField HeaderText="Marks">
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Eval("Marks") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMarks" runat="server" Text='<%# Eval("Marks") %>' Width="75"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<script type="text/javascript" src="js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="js/quicksearch.js"></script>
<script type="text/javascript">
$(function () {
$('.search_textbox').each(function (i) {
$(this).quicksearch("[id*=GridView1] tr:not(:has(th))", {
'testQuery': function (query, txt, row) {
return $(row).children(":eq(" + i + ")").text().toLowerCase().indexOf(query[0].toLowerCase()) != -1;
}
});
});
});
</script>
and code is
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lbl_AdNo = GridView1.Rows[e.RowIndex].FindControl("lbl_AdNo") as Label;
Label lbl_Sub = GridView1.Rows[e.RowIndex].FindControl("Label3") as Label;
TextBox Marks = GridView1.Rows[e.RowIndex].FindControl("txtMarks") as TextBox;
con = new SqlDbConnect();
con.SqlQuery("Update tblSetMarks set Marks=@Marks where SubjectID= @SubId and AdmissionNo=@AdNo");
con.Cmd.Parameters.Add(new SqlParameter("@SubId", lbl_Sub.Text));
con.Cmd.Parameters.Add(new SqlParameter("@AdNo", lbl_AdNo.Text));
con.Cmd.Parameters.Add(new SqlParameter("@Marks", Marks.Text));
con.NonQueryEx();
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnDataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
for (int i = 0; i < GridView1.Columns.Count; i++)
{
TableHeaderCell cell = new TableHeaderCell();
TextBox txtSearch = new TextBox();
txtSearch.Attributes["placeholder"] = GridView1.Columns[i].HeaderText;
txtSearch.CssClass = "search_textbox";
cell.Controls.Add(txtSearch);
row.Controls.Add(cell);
}
GridView1.HeaderRow.Parent.Controls.AddAt(0, row);
}