在DataGridView控件中加了一列,DataGridViewCheckBoxCell。通过下面的代码遍历checkbox控件,总是不能准确获得第一行的checkbox值,界面上选中了,但取出的值为false,或是取不到第一行的checkbox。代码如下:for(int i = 0;i<datagridview.Rows.Count;i++)
{
   DataGridViewCheckBoxCell c = datagridview.Rows[i].Cell[0] as DataGridViewCheckBoxCell;
   bool isChk = c.Selected;   //取值不对或报错未找到实例
}
请问这段代码哪错了?

解决方案 »

  1.   

       DataGridViewCheckBoxCell c = datagridview.Rows[i].Cells[0] as DataGridViewCheckBoxCell;
     
      

  2.   

    看了一下,datagridview.Rows[i].Cell[0]取出的类型是DataGridViewTextBoxCell 
    可是我的第一列是checkbox列啊。。
      

  3.   

    取完之后,确定一下c不是null。
      

  4.   

    是cells,我的笔误。(敲的,不是粘贴的。)
      

  5.   

    for(int i = 0;i<datagridview.Rows.Count;i++)
    {
       CheckBox c =(CheckBox)datagridview.Rows[i].Cell[0] ;
       bool isChk = c.Selected;   
    }是Winform程序还是Web啊?
      

  6.   

    加上一句 this.ValidateChildren(); 
    for(int i = 0;i<datagridview.Rows.Count;i++)
    {
       DataGridViewCheckBoxCell c = datagridview.Rows[i].Cell[0] as DataGridViewCheckBoxCell;
       bool isChk = c.Selected;   //取值不对或报错未找到实例
    }
      

  7.   

    for(int i = 0;i<datagridview.Rows.Count;i++)
    {
          bool isChk = this.datagridview.Rows[i].Cells[0].Value
    }