C# 在datagridview c中选择复选框#

C# 在datagridview c中选择复选框#,c#,winforms,datagridview,C#,Winforms,Datagridview,我在datagridview中有一个dowpdownlist、一个按钮和一个复选框 我只是在datagridview上手动创建了一个复选框列。 (这是代码) 以下是程序。 步骤1:用户将在复选框中选择项目。 步骤2:用户将在下拉列表中选择项目。 步骤3:用户将单击按钮,它将更改itemname 在下拉列表中所选项目之前的复选框上 这是我的问题 点击按钮后,什么也没有发生 这是我的密码 private void button1_Click(object sender, EventArgs e)

我在datagridview中有一个dowpdownlist、一个按钮和一个复选框

我只是在datagridview上手动创建了一个复选框列。 (这是代码)

以下是程序。
步骤1:用户将在复选框中选择项目。
步骤2:用户将在下拉列表中选择项目。
步骤3:用户将单击按钮,它将更改itemname
在下拉列表中所选项目之前的复选框上

这是我的问题 点击按钮后,什么也没有发生

这是我的密码

private void button1_Click(object sender, EventArgs e)
        {
    int x = 0;
                foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
                {
                    DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];
                    if (chk.Selected)
                    {
                    // codes here
                    }
                    else
                    {
                    //code here
                    }
                }
                x = x + 1;
         }

*已编辑**

我已经测试过了,它确实有效。将其复制并粘贴到新项目中,然后使用它。它会帮助你到达你需要的地方

   private void Form1_Load(object sender, EventArgs e)
    {

        DataGridViewCheckBoxColumn checkBox = new DataGridViewCheckBoxColumn(true);
        checkBox.HeaderText = "T/F";
        dataGridView1.Columns.Add(checkBox);

    }

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in dataGridView1.SelectedRows)
        {

            DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)row.Cells[0];

            if (Convert.ToBoolean(chk.Value) == true)
            {
                MessageBox.Show("Value Is True");
            }

        }
    }

我建议的第一件事是:

DataGrid1.EndEdit();
因为,我经历过,如果在从网格列检索checkbox值之前缺少这一行,则有时输入不会按预期显示

比如说:

private void button1_Click(object sender, EventArgs e)
{
    int x = 0;
    foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];

        if (chk.Value)
        {
              // codes here for checked condition
        }
        else
        {
              //code here  for UN-checked condition
        }
      }
     x = x + 1;
 }

按钮1\u单击
是否触发事件?如果不是,则可能没有将事件处理程序链接到按钮单击事件。是,它正在触发。当我使用断点时,它没有进入状态。this.DataGrid1.SelectedRows.Count的
值是多少?还有
chk
变量的用途。看起来没用。CheckState有错误。等于,即使我声明CheckState CheckState=default(CheckState);
private void button1_Click(object sender, EventArgs e)
{
    int x = 0;
    foreach (DataGridViewRow item in this.DataGrid1.SelectedRows)
    {
        DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)item.Cells[1];

        if (chk.Value)
        {
              // codes here for checked condition
        }
        else
        {
              //code here  for UN-checked condition
        }
      }
     x = x + 1;
 }