[英]How to delete or edit by using 2 check boxes in DataGridView?
我在DataGridView對象中顯示數據。 第一列代表刪除,第二列代表編輯,其余為數據。
我想做的是在選中其中一個復選框時刪除並編輯所選數據。
我對選擇一個復選框時如何做某事感到困惑,基本上是如何檢查單擊了哪個字段。
我該怎么做呢?
我有這個取消選中其他先前檢查的字段:
public MainWindow()
{
InitializeComponent();
dgvPC.CellContentClick += new DataGridViewCellEventHandler(ContentClick_CellContentClick);
}
void ContentClick_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
foreach (DataGridViewRow row in dgvPC.Rows)
{
row.Cells[Delete1.Name].Value = false;
row.Cells[Edit1.Name].Value = false;
}
}
我正在這樣添加數據:
if (security.DecryptAES(read.GetString(1), storedAuth.Password, storedAuth.UserName) == "PC Password")
{
// Count PC passwords.
countPC++;
dgvPC.Rows.Add(read.GetInt32(0), false, false,
security.DecryptAES(read.GetString(5), storedAuth.Password, storedAuth.UserName),
security.DecryptAES(read.GetString(6), storedAuth.Password, storedAuth.UserName));
}
從您的陳述中推斷,您無法確定正在考慮使用哪個復選框
因此,要縮小范圍,您應該嘗試使用網格的CellValueChanged
事件
void grd_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((sender as DataGridView).CurrentCell is DataGridViewCheckBoxCell)
{
//Now you know its a checkbox
//use the ColumnIndex of the CurrentCell and you would know which is the column
// check the state by casting the value of the cell as boolean
}
}
如果要立即執行操作,則必須提交該值,並且當焦點移出單元格時會發生這種情況。 嘗試處理網格的CurrentCellDirtyStateChanged
這樣的事情應該為您工作
void grd_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (grd.IsCurrentCellDirty)
{
grd.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.