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();
    }
}

 

 

posted @ 2012-11-20 17:29  北方狼  阅读(444)  评论(0编辑  收藏  举报