代码之家  ›  专栏  ›  技术社区  ›  Jonathan Escobedo

如何使用C 3.0从复选框列中获取值?

  •  0
  • Jonathan Escobedo  · 技术社区  · 16 年前

    我可以用这种方式获取当前选定的行:

     private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e){
    
    //Cells[0] cause CheckBoxColumn is in that index (first column)
    DataGridViewCheckBoxCell temp = (DataGridViewCheckBoxCell)dgv.Rows[e.RowIndex].Cells[0];
    }
    

    所以,现在我想获取用户检查过的所有行:

     foreach (var row_ in DataGridView1.Rows.OfType<DataGridViewRow>().
                                            Select(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
                                             Where(r => r.Value.Equals(true))).FirstOrDefault()){
    
    }
    

    我得到 null reference 来自调试器。

    我做错什么了?

    2 回复  |  直到 16 年前
        1
  •  1
  •   mqp    16 年前

    我怀疑你做错了,你真正想写的是:

    foreach (var row_ in
        DataGridView1.Rows.OfType<DataGridViewRow>().
        Where(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
        Any(r => r.Value.Equals(true))))
    {
    
    }
    

    但我不确定。

        2
  •  0
  •   Jonathan Escobedo    16 年前

    这就是答案,希望有帮助(感谢mquander提供。任何想法):

            foreach (var _row in dgvpendientepago.Rows.OfType<DataGridViewRow>().
                Where(o => o.Cells.OfType<DataGridViewCheckBoxCell>().
                    Any(r => r.EditedFormattedValue.Equals(true))))
            {
              // do stuff like the following : 
              lst4Pay.Add(new Cobranzaciaseguro
              {
                numeroatencion = Convert.ToInt16(_row.Cells[3].Value),
                estado = 'P'
              });
            }
    
    推荐文章