How to Delete multiple records from Gridview in one shot?

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 674
Today, i will tell you how to Delete multiple records from Gridview in one shot without polling database again & again in looping. Just take StringBuilder and append Id with comma.

Our aspx page will look like this:-

<asp:GridView ID="grid_view_employee_details" runat="server" AllowPaging="true" PageSize="5" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chk_employee_id" runat="server" />
<asp:Label ID="lbl_employee_id" runat="server" Text='<%#Eval("id") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Employee Code">
<ItemTemplate>
<asp:Label ID="lbl_employee_code" runat="server" Text='<%#Eval("employee_code") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Employee Name">
<ItemTemplate>
<asp:Label ID="lbl_employee_name" runat="server" Text='<%#Eval("employee_name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btn_delete" runat="server" Text="Button" OnClick ="btn_delete_Click" />

In the page load:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
fill_grid();
}
catch (Exception ex)
{
throw ex;
}
}
}

private void fill_grid()
{
try
{
DataTable dt_employee = new DataTable();
dt_employee.Columns.Add("ID");
dt_employee.Columns.Add("Employee_Code");
dt_employee.Columns.Add("Employee_Name");

dt_employee.Rows.Add("1", "ABC00", "Vishu");
dt_employee.Rows.Add("2", "ABC01", "Dinesh");
dt_employee.Rows.Add("3", "ABC02", "Rajesh");
dt_employee.Rows.Add("4", "ABC03", "Neeraj");
dt_employee.Rows.Add("5", "ABC04", "Nitin");

grid_view_employee_details.DataSource = dt_employee;
grid_view_employee_details.DataBind();
}
catch (Exception ex)
{
throw ex;
}
}

In the btn_delete event:-

protected void btn_delete_Click(object sender, EventArgs e)
{
System.Text.StringBuilder sb_emp_id = new System.Text.StringBuilder();
CheckBox chk_employee_id = null;
Label lbl_employee_id = null;
string emp_ids = string.Empty;

try
{
if (grid_view_employee_details.Rows.Count > 0)
{
foreach (GridViewRow gvr in grid_view_employee_details.Rows )
{
chk_employee_id = gvr.FindControl("chk_employee_id") as CheckBox;
lbl_employee_id = gvr.FindControl("lbl_employee_id") as Label;

if (chk_employee_id != null && lbl_employee_id != null)
{
if (chk_employee_id.Checked && !string.IsNullOrEmpty(lbl_employee_id.Text))
{
sb_emp_id.AppendFormat("{0},", lbl_employee_id.Text.Trim());
}
}
}

emp_ids = sb_emp_id.ToString();

if (!string.IsNullOrEmpty(emp_ids))
{
emp_ids = emp_ids.TrimEnd(',');

//write delete statement and pass emp_id in IN clause as
string sql_query = "delete from table_name where id in('" + emp_ids + "')";
//write rest of the code
}
else
{
//show message that says please select at-least one checkbox
return;
}
}
}
catch (Exception ex)
{
throw ex;
}
}

Comments or Responses

Posted by: t5j9033387989 on: 11/26/2013 Level:Starter | Status: [Member] | Points: 10
intersting...thanks...

Login to post response