資料來源:http://www.dotblogs.com.tw/nui_kni/archive/2008/12/15/6394.aspx#6459
後端:
protected void mygDB()
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=mydata;User Id=sa;Password=1234");
conn.Open();
SqlCommand cmd = new SqlCommand("Select Item_ID,Item_Name,Quantity,Tracking,Expired From Item_Detail", conn);
// SqlCommand cmd = new SqlCommand("Select Item_ID,Item_Name,Quantity,Tracking,Expired From Item_Detail Where Account_Name='"+Session["Account_Name"]+"'", conn);
SqlDataReader dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt.AsDataView();
GridView1.DataBind();
}
else
{
Label2.Text = "#還沒上傳物品或已下架";
GridView1.DataSource = dt.AsDataView();
GridView1.DataBind();
}
cmd.Dispose();
conn.Close();
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=mydata;User Id=sa;Password=1234");
conn.Open();
string src;
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)row.Cells[0].FindControl("CheckBox1");
Label lbID = (Label)row.FindControl("Label3");
if (cb.Checked)
{
src = lbID.Text.ToString();
SqlCommand cmd = new SqlCommand(string.Format("Delete From Item_Detail Where Item_ID={0}", src), conn);
cmd.ExecuteNonQuery();
cmd.Cancel();
}
}
}
conn.Close();
mygDB();