Answered by:
Row deleting event call outside button from gridview ?

Question
-
User-367318540 posted
I want to call gridview row deleting event behind outside button from gridview ?
Thursday, January 2, 2020 5:53 AM
Answers
-
User288213138 posted
Hi akhterr,
i just have one row to delete and i am not inserting more rows into gridviewAgain, in the RowDeleting event, the row is deleted according to the button in each row. If you want to call RowDeleting event, it is difficult to find the button of the deleted row.
If you want to delete a row, then you can use the following code directly.
protected void Button1_Click(object sender, EventArgs e) { DataTable dt = ViewState["dt"] as DataTable; for (int i = dt.Rows.Count - 1; i >= 0; i--) { DataRow dr = dt.Rows[i]; if (dr["Id"].ToString() == "You want to delete the id of that row") { dr.Delete(); Bind(); } } }
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 2, 2020 8:43 AM
All replies
-
User-1716253493 posted
Deleting event happened when button with commandname="delete" in spesific row clicked. So, how to determine wicth row has clicked when you click from outside the grid?
Please elaborate your question.
You can use outside button click to delete data from the grid. Place checkbox in itemtemplate, in button click event loop the rows. When the checkbox is checked, then get the id value of the checked row to delete the data.
Thursday, January 2, 2020 6:55 AM -
User288213138 posted
Hi akhterr,
akhterr
I want to call gridview row deleting event behind outside button from gridview ?According to your description, i made demo for you. But if you want to call the Row deleting event, you are not sure which line you want to delete.
I delete the rows in Gridview by the textbox value.
<asp:GridView ID="GridView1" runat="server"></asp:GridView> delete row:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" /> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(); } } public void Bind() { string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; string query = "SELECT * FROM Customers"; using (SqlConnection con = new SqlConnection(constr)) { using (SqlDataAdapter sda = new SqlDataAdapter(query, con)) { using (DataTable dt = new DataTable()) { sda.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } } } } protected void Button1_Click(object sender, EventArgs e) { for (int rowIndex = 0; rowIndex < GridView1.Rows.Count - 1; rowIndex++) { int customerId = Convert.ToInt32(TextBox1.Text); string query = "DELETE FROM Customers WHERE CustomerId=@CustomerId"; string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand(query)) { cmd.Parameters.AddWithValue("@CustomerId", customerId); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } Bind(); }
The result:
Best regards,
Sam
Thursday, January 2, 2020 6:59 AM -
User-367318540 posted
Hi sam,
I do not want to delet from database ,datatable have to delete
below is row deleting event of gridview ,i want outside button
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e) { int index = Convert.ToInt32(e.RowIndex); DataTable dt = ViewState["dt"] as DataTable; dt.Rows[index].Delete(); ViewState["dt"] = dt; BindGrid(); }
Thursday, January 2, 2020 7:02 AM -
User288213138 posted
Hi akhterr,
Row deleting event call outside button from gridviewIf you want to call the RowDeleting event, So how do you choose which line you want to delete?
So i suggest you can delete the row in button click event directly.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDeleting="GridView1_RowDeleting"> <Columns> <asp:BoundField DataField="Id" HeaderText="Id"/> <asp:BoundField DataField="Name" HeaderText="Name"/> <asp:BoundField DataField="Age" HeaderText="Age"/> <asp:CommandField ShowDeleteButton="true" /> </Columns> </asp:GridView> delete row:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /> <asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" /> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Age") }); dt.Rows.Add(1, "name1", "age1"); dt.Rows.Add(2, "name2", "age2"); dt.Rows.Add(3, "name3", "age3"); ViewState["dt"] = dt; Bind(); } } public void Bind() { GridView1.DataSource = (DataTable)ViewState["dt"]; GridView1.DataBind(); } protected void Button1_Click(object sender, EventArgs e) { DataTable dt = ViewState["dt"] as DataTable; for (int i = dt.Rows.Count - 1; i >= 0; i--) { DataRow dr = dt.Rows[i]; if (dr["Id"].ToString() == TextBox1.Text) { dr.Delete(); Bind(); }
The result:
Best regards,
Sam
Thursday, January 2, 2020 7:52 AM -
User-367318540 posted
i just have one row to delete and i am not inserting more rows into gridview
Thursday, January 2, 2020 8:19 AM -
User-367318540 posted
i used your code but row not getting delete from gridview,please review code
protected void btnClear_Click(object sender, EventArgs e) { DataTable dtp = ViewState["dtp"] as DataTable; for (int i = dtp.Rows.Count - 1; i >= 0; i--) { DataRow dr = dtp.Rows[i]; if (dr["Prdno"].ToString() == txtno.Text) { dr.Delete(); Bind(); } insertintopacktxt(); }
Thursday, January 2, 2020 8:27 AM -
User288213138 posted
Hi akhterr,
i just have one row to delete and i am not inserting more rows into gridviewAgain, in the RowDeleting event, the row is deleted according to the button in each row. If you want to call RowDeleting event, it is difficult to find the button of the deleted row.
If you want to delete a row, then you can use the following code directly.
protected void Button1_Click(object sender, EventArgs e) { DataTable dt = ViewState["dt"] as DataTable; for (int i = dt.Rows.Count - 1; i >= 0; i--) { DataRow dr = dt.Rows[i]; if (dr["Id"].ToString() == "You want to delete the id of that row") { dr.Delete(); Bind(); } } }
Best regards,
Sam
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, January 2, 2020 8:43 AM -
User-367318540 posted
Thanks alot samwu,
Thursday, January 2, 2020 8:46 AM