DataGridView 单选和值变化事件

1.单选一行

     设置DataGridView的属性MultiSelect为false

    DataGridViewCheckBoxCell列只能勾选一行

 

勾选一行
     private void DgvGrid_CellContentClick(object sender, DataGridViewCellEventArgs e)
       {
            int count = DgvGrid.Rows.Count;
            for (int i = 0; i < count; i++)
            {
                DataGridViewCheckBoxCell check = (DataGridViewCheckBoxCell)DgvGrid.Rows[i].Cells[0];
                Boolean flag = Convert.ToBoolean(check.Value);
                if (flag == true)
                {
                    check.Value = false;
                }
                else
                {
                    continue;
                }
            }
        }

 

2 列值变化立即触发CellValueChanged事件

 

 

CellValueChanged事件是在列值变化后触发,若要实现即时触发应同时使用CurrentCellDirtyStateChanged事件;

以下为MSDN代码

CellValueChanged
// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
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 == "CheckBoxes")
    {
        DataGridViewDisableButtonCell buttonCell =
            (DataGridViewDisableButtonCell)dataGridView1.
            Rows[e.RowIndex].Cells["Buttons"];

        DataGridViewCheckBoxCell checkCell =
            (DataGridViewCheckBoxCell)dataGridView1.
            Rows[e.RowIndex].Cells["CheckBoxes"];
        buttonCell.Enabled = !(Boolean)checkCell.Value;

        dataGridView1.Invalidate();
    }
}

 

 

转载于:https://www.cnblogs.com/golddragonxu/archive/2012/11/20/2779408.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值