简体   繁体   English

更改后获取DataGridView复选框值

[英]Get DataGridView checkbox value after changed

i have DataGridView with several columns, every Row represent my object with several properties like name, id etc. after change my Name column Checkbox i want to update my object How can i get my column Checknox state ? 我有几个列的DataGridView ,每行代表我的对象有几个属性,如名称,ID等更改我的Name column Checkbox我想更新我的对象如何获得我的列Checknox状态? This is what i have try: 这是我尝试过的:

private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "Name")
    {

    }
}

I assume Name is the name of DataGridViewCheckBoxColumn 我假设NameDataGridViewCheckBoxColumn的名称

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "Name")
    {
        var isChecked = dataGridView1[e.ColumnIndex, e.RowIndex].Value as bool? ?? false;
        //rest of logic
    }
}

this is the simplest and safest way. 这是最简单,最安全的方式。

To iterate through all cells in row use this code: 要遍历行中的所有单元格,请使用以下代码:

DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
foreach (DataGridViewCell cell in row.Cells)
{
   object value = cell.Value;
}

Maybe you can use the FindControl() to get the state of checkbox: 也许您可以使用FindControl()来获取复选框的状态:

CheckBox cbName = (CheckBox)dataGridView1.Columns[e.ColumnIndex].FindControl("cbName");
checkState = cbName.CheckState.toString();

or 要么

isChecked = cbName.Checked;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM