دوست عزیز از این کد استفاده کن
متد خذف سطر
#region Multiple Delete
private void DeleteRecords(StringCollection sc)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
StringBuilder sb = new StringBuilder(string.Empty);
foreach (string item in sc)
{
const string sqlStatement = "DELETE FROM Customers WHERE CustomerID";
sb.AppendFormat("{0}='{1}'; ", sqlStatement, item);
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Deletion Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
#endregion
کد مربوط به باتنی که کار حذف رو انجام میده
protected void ButtonDelete_Click(object sender, EventArgs e)
{
StringCollection sc = new StringCollection();
string id = string.Empty;
for (int i = 0; i < GridView1.Rows.Count; i++)//loop the GridView Rows
{
CheckBox cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); //find the CheckBox
if (cb != null)
{
if (cb.Checked)
{
id = GridView1.Rows[i].Cells[1].Text; // get the id of the field to be deleted
sc.Add(id); // add the id to be deleted in the StringCollection
}
}
}
DeleteRecords(sc); // call method for delete and pass the StringCollection values
BindGridView(); // Bind GridView to reflect changes made here
}
کد رویداد RowDataBound مربوط به گرید ویو
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header) //check for RowType
{
Button b = (Button)e.Row.FindControl("ButtonDelete"); //access the LinkButton from the Header TemplateField using FindControl
b.Attributes.Add("onclick", "return ConfirmOnDelete();"); //attach the JavaScript function
}
}