日期:2014-05-19  浏览次数:20759 次

请教个关于toolstrip的问题
小弟请教个问题,希望各位大大能帮忙下,谢谢了。
我在同一个窗体下,放了一个DataGridView、一个button和一个toolstrip,在toolstrip里添加了一个新button,DataGridView里有2列,一列是DataGridViewCheckBoxColumn,一列是DataGridViewTextBoxColumn。
我的想法很简单,就是在点中DataGridViewCheckBoxColumn打上 "√ "的时候,我点击Button后可以取到该行第二列的值,判断是否点中的方法就是:
  for   (int   i   =   0;   i   <   Int32.Parse(this.DataGridview1.Rows.Count.ToString());   i++)
  {
        if   (this.DataGridview1.Rows[i].Cells[0].FormattedValue.ToString()   ==   "True ")
    {
        string     str   =   this.dg1.Rows[i].Cells[1].FormattedValue.ToString();
    }
}

因为当DataGridViewCheckBoxColumn被选中 "√ "的时候,它的FormattedValue为true,不选则为false.所以我是通过FormattedValue来判断是否选中该行的。

下面我来说问题,我在窗体上直接放的button的click事件里写这些代码,可以正常判断出true,可在toolstrip里添加的button的click事件里写这些代码的时候,被选中的DataGridViewCheckBoxColumn就始终是false,同一个窗体,同样的代码,取同一个列的值,为什么就不一样呢,是不是toolstript的问题呢?

------解决方案--------------------
估计是编辑状态问题。
在toolstript的按钮事件开始,加入:
DataGridview1.EndEdit();

另外i < Int32.Parse(this.DataGridview1.Rows.Count.ToString())人为的增加了装箱拆箱
,改成i < DataGridview1.Rows.Count 可以提高效率