The effect to be achieved is very simple. For more information, see:
Function: Select the checkbox, and then delete the selected items from the database.ArticleIt focuses on how to implement batch deletion and does not explain how to connect to the database and bind data in detail.
1 first, add a column as checkbox in the gridview.CodeAs follows:Copy codeThe Code is as follows: <asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false">
<Columns>
<Asp: boundfield datafield = "vote_id" headertext = "no."/>
<Asp: boundfield datafield = "vote_name" headertext = "name"/>
<Asp: templatefield headertext = "select">
<Itemtemplate>
<Asp: checkbox id = "cbxid" runat = "server"/>
</Itemtemplate>
</ASP: templatefield>
</Columns>
</ASP: gridview>
Note that the ID is cbxid and will be used later.
2. Bind data.Copy codeThe Code is as follows: // initialize data
Private void datainit ()
{
String sqltext = "select * From vote ";
Sqlconnection conn = getcon ();
Sqldataadapter da = new sqldataadapter (sqltext, Conn );
Dataset DS = new dataset ();
Conn. open ();
Da. Fill (DS, "Vote ");
Gridview1.datasource = Ds;
Gridview1.datakeynames = new string [] {"vote_id "};
Gridview1.databind ();
Conn. Close ();
Conn. Dispose ();
}