GridView Bulk Delete record, select All and Eject confirmation dialog box

Source: Internet
Author: User
The GridView control allows the user to edit the template by itself so that it can add functionality, define formats, and so on as if it had previously been mixed programming, and for a large number of records it is tiring work to delete a single item. It is natural to add a bulk deletion feature to it. Before you work, you must understand the problem of the GridView primary key.

1.GridView PRIMARY Key

If it's not wise to display everything from a data source in Girdview, the minimum primary key should be hidden. So when editing, you do not need to specify attributes such as ReadOnly, the display will be more ideal (believe that many primary key values should be numeric numbers). If you hide the primary key (Edit column setting visible=false), we certainly have no way to get the primary key value Gridview1.rows[i] Using a statement like this. Cells[0]. Text. This approach is mainstream in the DataGrid age. But in the GridView this method is different, but 2. X provides DataKeys and DataKeyNames properties , which is more convenient than a DataGrid.

Set a CSS style . Hidden{display:none} to assign it to the HeaderStyle, FooterStyle, and ItemStyle CssClass properties of the primary key. The primary key columns are not displayed at run time. You need to specify the primary key for the GridView control when you bind the data source:

Gridview1.datasource = ds. tables["Categories"];
gridview1.datakeynames = new string[] {"CategoryID"};
Gridview1.databind ();

It's easy to use:gridview1.datakeys[i]. Value.tostring (), which returns the primary key value, where I is the line number of the currently selected row. You can now use this method for bulk deletion.

2. Bulk deletion

In the case of bulk deletion, a list of selection boxes is required to allow the user to select items to delete. It is only necessary to add a column to the first column or column in the GridView. Inside you can edit the template to add the controls you want. The specific HTML code is shown below.


onclick= "Button1_Click" text= "delete" />
onclick= "button2_click" text= "All selected" />

3. Confirm before deleting
Use the Button1 button here to delete the task, which requires the user to make a final confirmation before the deletion, just add the following line to the Page_Load event:
Button1.Attributes.Add ("onclick", "Return to confirm (' Are you sure? ')");
4. Select All Records
When the user clicks Button2, if text is selected, all rows are selected and the button is set to cancel, and then click Button2 again to cancel all selections. The code is as follows
protected void button2_click (object sender, EventArgs e)
{
CheckBox CB;
if (Button2.text = "All selected")
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CB = (CheckBox) gridview1.rows[i]. Cells[0]. FindControl ("Del");
Cb. Checked = true;
}
Button2.text = "Cancel";
}
Else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
cb = (CheckBox) gridview1.rows[i]. Cells[0]. FindControl ("Del");
Cb. Checked = false;
}
Button2.text = "All election";
}
}
where del is the ID value of the checkbox

5. Full source

public partial class DelAll:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
Loadgridview ();
}
Button1.Attributes.Add ("onclick", "Return to confirm (' Are you sure? ')");
}
protected void Button1_Click (object sender, EventArgs e)
{
String sql = "Delete from Categories where";
String cal = "";
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox cb = (checkbox) Gridview1.rows[i]. Cells[0]. FindControl ("Del");
if (CB). Checked==true)
{
Cal = + "categoryid=" + gridview1.datakeys[i]. Value.tostring () + "or";
}
}
if (Cal!= "")
{
SQL + + cal. Substring (0, Cal. LENGTH-3);
}
Else
{
sql = "";//Do not delete
}
Response.Write (SQL);//Here you can define your own programs, delete tasks
}
private void Loadgridview ()
{
String strconn = "Server=localhost//sqlexpress;database=northwind;user id=rayshow;password=ray123";
SqlConnection conn = new SqlConnection (strconn);
DataSet ds = new DataSet ();
SqlDataAdapter da = new SqlDataAdapter ("select * from Categories", conn);
Da. Fill (ds, "Categories");
Gridview1.datasource = ds. tables["Categories"];
gridview1.datakeynames = new string[] {"CategoryID"};
Gridview1.databind ();
}
protected void button2_click (object sender, EventArgs e)
{
CheckBox CB;
if (Button2.text = "All selected")
{

for (int i = 0; i < GridView1.Rows.Count; i++)
{
CB = (CheckBox) gridview1.rows[i]. Cells[0]. FindControl ("Del");
Cb. Checked = true;
}
Button2.text = "Cancel";
}
Else
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CB = (CheckBox) gridview1.rows[i]. Cells[0]. FindControl ("Del");
Cb. Checked = false;
}
Button2.text = "All election";
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.