简体   繁体   English

DataGridViewCheckBoxColumn-如果复选框已选中

[英]DataGridViewCheckBoxColumn - If checkBox checked issue

I have this code down below which should show messageBox when the checkbox in checkbox column is checked. 我将这段代码放到下面,当选中“复选框”列中的复选框时,该代码应显示messageBox。 It is test for me that I know that the row was really selected. 对我而言,我知道该行确实已被选中是一个考验。

If this would work I'm going to save SelectedRows into DB. 如果这行得通,我将把SelectedRows保存到数据库中。 So maybe its helpful to know when building this code. 因此,在构建此代码时了解它可能会有所帮助。 As I'm begginer I wanted to ask you guys why MessageBox doesnt apper when I check checkBox? 因为我是初学者,所以我想问大家,当我选中checkBox时,为什么MessageBox没有包装? Thanks so much in advance. 非常感谢。

  DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
  dtg_ksluzby.Columns.Add(chk);
  dtg_ksluzby.Columns[3].Width = 20;

  foreach (DataGridViewRow row in dtg_ksluzby.Rows)
  {
      // number 3 represents the 4th column of dgv
      DataGridViewCheckBoxCell chk1 = row.Cells[3] as DataGridViewCheckBoxCell; 
      if (Convert.ToBoolean(chk1.Value) == true)
      {
         MessageBox.Show("this cell checked");
      }
      else
      {
      }
  }

This code will never hit the message box code - you've created the control, added it to the table, then immediately checked them for their values, which will be not set. 此代码将永远不会触及消息框代码-您已经创建了控件,将其添加到表中,然后立即检查它们的值(未设置)。

You need to have an event handler that catches changed values in the datagridview: 您需要有一个事件处理程序,可以在datagridview中捕获更改的值:

private void dtg_ksluzby_CellValueChanged(object sender, 
                                          DataGridViewCellEventArgs e)
{
    // Check through the cells here (or use event args to get data)
}

暂无
暂无

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

相关问题 为什么未选中DatagridviewCheckboxColumn的复选框? - Why isn't DatagridviewCheckboxColumn's checkbox getting checked? 选中第一个checkBox列时获取两个DataGridViewCheckBoxColumn - Getting two DataGridViewCheckBoxColumn when first checkBox column is checked 以编程方式检查 DataGridViewCheckBoxColumn 中的单元格 - Cell from DataGridViewCheckBoxColumn Checked programmatically 网格视图中复选框选中的问题 - Checkbox checked issue in grid view 选中或未选中DataGridViewCheckBoxColumn上的C#datagridview过滤器 - C# datagridview filter on DataGridViewCheckBoxColumn checked or unchecked 如何在DatagridviewCheckBoxColumn的Header添加复选框控件? - How to add checkbox control at the Header of DatagridviewCheckBoxColumn? DataGridViewCheckBoxColumn问题开始查看复选框是否已选中 - DataGridViewCheckBoxColumn issues getting to see if the checkbox selected Datagridview使用以下方法在某些单元格上添加复选框 <DataGridViewCheckBoxColumn> ,而不是专栏 - Datagridview add Checkbox on some cell using <DataGridViewCheckBoxColumn>, not a column Windows窗体 - &gt; DataGridView-&gt; DataGridViewCheckBoxColumn取消选中所有留下的一个项目已选中 - Windows Form->DataGridView->DataGridViewCheckBoxColumn Uncheck All Leaves One Item Checked 防止用户检查dataGridViewcheckBoxColumn(如果已选中)并将特定单元格内容存储在变量中,具体取决于检查的行 - prevent user from checking dataGridViewcheckBoxColumn if one selected and store specific cell content in a variable depending on the checked row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM