Hi indradeo,
Refer below sample.
HTML
<asp:GridView ID="gvCustomersRow" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Flw_mtr" HeaderText="Flw_mtr" />
<asp:BoundField DataField="param_dt" HeaderText="param_dt" />
<asp:BoundField DataField="rd_One" HeaderText="rd_One" />
<asp:BoundField DataField="rd_Two" HeaderText="rd_Two" />
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:Button Text="Edit" runat="server" OnClick="OnEdit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:HiddenField ID="hfCurrentRowId" runat="server" />
<asp:HiddenField ID="hfNextRowId" runat="server" />
<asp:TextBox ID="txtNew" runat="server" />
<asp:Button ID="btnUpdate" runat="server" Text="Update" OnClick="OnUpdate" />
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGridView();
}
}
private void BindGridView()
{
string conString = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT Id, Flw_mtr, param_dt, rd_one, rd_Two FROM CustomersRow", con))
{
con.Open();
gvCustomersRow.DataSource = cmd.ExecuteReader();
gvCustomersRow.DataBind();
con.Close();
}
}
}
protected void OnEdit(object sender, EventArgs e)
{
GridViewRow currentRow = (sender as Button).NamingContainer as GridViewRow;
if (currentRow.RowIndex < gvCustomersRow.Rows.Count - 1)
{
GridViewRow nextRow = gvCustomersRow.Rows[currentRow.RowIndex + 1];
hfCurrentRowId.Value = currentRow.Cells[0].Text;
hfNextRowId.Value = nextRow.Cells[0].Text;
txtNew.Text = currentRow.Cells[4].Text;
}
}
protected void OnUpdate(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(hfCurrentRowId.Value) && !string.IsNullOrEmpty(hfNextRowId.Value))
{
string query = "Update CustomersRow SET rd_Two = @NewValue WHERE Id = @CurrentId ";
query += "Update CustomersRow SET rd_One = @NewValue WHERE Id = @NextId";
string conString = ConfigurationManager.ConnectionStrings["Constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@CurrentId", hfCurrentRowId.Value);
cmd.Parameters.AddWithValue("@NextId", hfNextRowId.Value);
cmd.Parameters.AddWithValue("@NewValue", txtNew.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
this.BindGridView();
hfCurrentRowId.Value = string.Empty;
hfNextRowId.Value = string.Empty;
txtNew.Text = string.Empty;
}
Screenshot