datagridview的checkbox列,當修改checkbox狀態時觸發

主要用到了datagridview的CurrentCellDirtyStateChanged和CellValueChanged兩個事件

CurrentCellDirtyStateChanged事件是提交對checkbox狀態的修改

CellValueChanged事件是當狀態提交後,也就是單元格值改變後做一些其它的操作,這裏是將checkbox列的true或false狀態作爲tooptiptext屬性設置到同一行的button列

 

CurrentCellDirtyStateChanged事件代碼 :


private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (this.dataGridView1.IsCurrentCellDirty) //有未提交的更//
{
                
this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
 }
 }

 

CellValueChanged事件代碼 :


 private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            
if (this.dataGridView1.Columns[e.ColumnIndex].Name.Equals("gender"))
            {
                DataGridViewButtonCell dgvButtonCell 
= this.dataGridView1.Rows[e.RowIndex].Cells["btn"as DataGridViewButtonCell;//獲得button列單元格
                DataGridViewCheckBoxCell dgvCheckBoxCell = this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewCheckBoxCell;//獲得checkbox列單元格
                dgvButtonCell.ToolTipText = dgvCheckBoxCell.Value.ToString();//賦值
            }
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章