在winform中使用checbox很多。上次那个项目里就用到了,写了一个不太好用,后来翻阅了一下微软提供的样码,我觉得有必要给大家分享一下。

        // This event handler manually raises the CellValueChanged event
        // by calling the CommitEdit method.
        public void DataGridView1_CurrentCellDirtyStateChanged(object sender,
            EventArgs e)
        {
            if (DataGridView1.IsCurrentCellDirty)
            {
                DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
            }
        }

        // If a check box cell is clicked, this event handler disables  
        // or enables the button in the same row as the clicked cell.
        public void DataGridView1_CellValueChanged(object sender,
            DataGridViewCellEventArgs e)
        {
            if (DataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxs")
            {


                DataGridViewCheckBoxCell checkCell =
                    (DataGridViewCheckBoxCell)DataGridView1.
                    Rows[e.RowIndex].Cells["CheckBoxs"]; 
                if ((Boolean)checkCell.Value)
                { 
                     //处理选中
//do something
} DataGridView1.Invalidate(); } }

 

相关文章:

  • 2022-12-23
  • 2022-01-15
  • 2022-02-11
  • 2022-12-23
  • 2021-11-15
猜你喜欢
  • 2021-07-17
  • 2021-12-09
  • 2022-01-14
  • 2022-01-11
  • 2022-12-23
  • 2021-12-20
相关资源
相似解决方案