datagridview清空資料庫
⑴ 如何清空C#中dataGridView的某一行數據
先把一個DataTable和資料庫的表綁定,然後
DataGridView.DataSource = DataTable
刪除時
((DataTable)DataGridView.DataSource).Rows[行號].Delete();
更新時把((DataTable)DataGridView.DataSource)作為DataTable去更新資料庫就行。
⑵ DataGridView中刪除選中的整行數據,但要同時刪除資料庫里的數據,要怎麼寫 我用的是sql
DataGridView是有綁定 dataset 或者datatable 的,你要把數據源裡面的那一行刪除,然後重新綁定或者刷新下DataGridView 就可以了
同時還要寫語句刪除資料庫~
如:
SqlCommand cmd = new SqlCommand();
cmd.Connection = new SqlConnection(sql_connect);
cmd.CommandText = "delete from gl_card_no where card_no = '" + textBox3.Text.Trim() + "'";
if (MessageBox.Show("你確定刪除該行么?" + cmd.CommandText + "", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{
try
{
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
MessageBox.Show("刪除成功!");
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
finally
{
cmd.Dispose();
}
}
⑶ .net 中datagridview 中如何刪除多行,同時刪除資料庫
要批量刪除的話,一般就是設置一下gridview的樣式,在每一行上放入一個復選框,選中哪個復選框就刪除哪行。
protected void btnAll_Click(object sender, EventArgs e)
{
//全選用
int rowchk = grdMyCart.Rows.Count;
if (btnAll.Text == "全 選")
{
btnAll.Text = "全不選";
for (int i = 0; i < rowchk; i++)
{
CheckBox chk = (CheckBox)grdMyCart.Rows[i].FindControl("chSeclect");
chk.Checked = true;
}
}
else
{
btnAll.Text = "全 選";
for (int i = 0; i < rowchk; i++)
{
CheckBox chk = (CheckBox)grdMyCart.Rows[i].FindControl("chSeclect");
chk.Checked = false;
}
}
}
protected void btnDel_Click(object sender, EventArgs e)
{
//////-------刪除產品信息---------
ArrayList rowCel = new ArrayList();
int rowCount = grdMyCart.Rows.Count;
//循環得到選中產品的編號
for (int i = 0; i < rowCount; i++)
{
CheckBox chDel = (CheckBox)grdMyCart.Rows[i].FindControl("chSeclect");
if (chDel.Checked == true)
{
rowCel.Add(grdMyCart.Rows[i].Cells[1].Text);
}
}
//調用數據訪問類的刪除方法
if (rowCel.Count > 0)
{
int resault = Delcardata(rowCel);
lblMessage.Text = "成功刪除了 " + resault + " 條記錄!";
}
else
{
Response.Write("<script language=\"javascript\">window.alert(\"請選擇要刪除的書籍!\")</script>");
}
//重新讀取購物車信息
displayData();
}
這是我以前找到過的代碼片段,你看看
⑷ 各位大哥,求在datagridview中刪除選中行,並刪除資料庫中數據的代碼!
你是選中行,能獲取到該行的id號吧,然後你直接就執行刪除資料庫記錄就行了
,你可以把刪除寫個存儲過程,傳進去id刪除該記錄,然後你重新對datagridview進行數據綁定就可以了
⑸ 各位大哥,求在datagridview中刪除選中行,並刪除資料庫中數據的代碼!
簡單:
下面是按鍵下代碼,你自己改!
SqlConnection _con = new SqlConnection(@"Data Source=192.168.0.34\WTMT;Initial Catalog=WtmtDatabase;User ID=sa;pwd=wtmt");
SqlCommand _cmd;
try
{
if (this.dataGridView1.Focused)
{
string strName = this.dataGridView1.CurrentRow.Cells["mp_LotNumber"].Value.ToString().Trim();
try
{
DialogResult dr = MessageBox.Show(this, "您確定要刪除 : <" + strName + "> 該跟單信息嗎?", "刪除提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
int rowCount = 0; //存儲受影響行數
string sqlText = "delete from mainProcess where mp_LotNumber = '" + strName + "'";
_con.Open();
_cmd = new SqlCommand(sqlText , _con);
rowCount = _cmd.ExecuteNonQuery();
if(rowCount != 0)
{
MessageBox.Show("刪除成功!");
_cmd.close();
_con.close();
}
else
{
_cmd.close();
_con.close();
MessageBox.Show("刪除失敗!");
}
}
}
catch (Exception se)
{
MessageBox.Show(se.Message);
}
}
else
{
MessageBox.Show("請您在跟單信息報表中選擇您要刪除的跟單名稱!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
catch (Exception)
{
MessageBox.Show("所選數據為空!請先選擇數據再操作!");
}
有疑問可以直接HI我
⑹ 在datagridview中刪除行同時也刪除資料庫中數據該怎麼解決
根據你選擇行,進行資料庫刪除操作就可以;比如說你獲得選擇行的ID,再根據這些ID進行刪除操作