Here is ur answer.
Ive created a table :
Table Name : yMail
Field : id (int) | sender(varchar) | subject(varchar)
Now pick a GridView, Connect it to the table by selecting all the fields.
- Go to the 'Edit-Columns' of the GridView.
- Add a 'template-field' (push it up to the top using the arrow mark)
- Also, convert the 'id' field into 'Template-Field' (using Convert to Template)
- Hit OK.
- Now, go to the 'Edit-Template' of the GridView.
- In the 'Column[0]' of the Item-Template, drag-drop a CheckBox
- Check out whats the ID of the CheckBox (must be CheckBox1)
- Also, check the 'Column[1]' of the 'Item-Template
- You'll see a Label; check the ID of the Label (must be Label1)
- That's it.
Now Stop 'Editing Template'
Drag-Drop a Button on the Page & name it as Delete.
Double-Click the button & include the following code in the Code-Behing of the Double-Click event.
***************************************************
protected void Button1_Click(object sender, EventArgs e)
{
string str = "";
foreach (GridViewRow gr in GridView1.Rows)
{
CheckBox cb = (CheckBox)gr.Cells[0].FindControl("CheckBox1");
if (cb.Checked)
{
Label lbl = (Label)gr.Cells[1].FindControl("Label1");
// Delete
str = "delete from yMail where id=" + lbl.Text;
SqlDataSource1.DeleteCommand = str;
SqlDataSource1.Delete();
}
}
}
***********************************************************
Ya, this will help u.
Note : If u dont want id to be displayed in ur GridView, then u can set the 'id' field visibility to false, in 'Edit-Colums' option.
But still u can access the Label Filed