2009-04-24 96 views
1

我正在创建DataGridViewEx类,它继承自DataGridView。
我想创建一个当前行中选择第一个单元格的方法,所以我写了这个:DataGridView:如何在MultiSelect为true时选择当前行中的第一个单元格

 /// <summary> 
    /// The method selects first visible cell in a current row. 
    /// If current row is null, nothing is done. 
    /// If any cells are selected, they are unselected before selecting first cell. 
    /// </summary> 
    public void SelectFirstCellInCurrentRow() 
    { 
     if (CurrentRow == null) return; 

     ClearSelection(); 
     foreach (DataGridViewCell cell in CurrentRow.Cells) 
     { 
      if (cell.Visible) 
      { 
       cell.Selected = true; 
       return; 
      } 
     } 
    } 

而且我要这样使用它,例如:

 private void btnAdd_Click(object sender, EventArgs e) 
     { 
      bindingSource.Add(new Customer()); 
      bindingSource.MoveLast(); 
      grid.SelectFirstCellInCurrentRow(); 
      grid.BeginEdit(false); 
     }

我的任务是向网格添加新行并开始编辑其第一个单元格。
此代码工作正常,如果grid.MultiSelect = false,但如果grid.MultiSelect = true,那么它不会按预期工作:取消所有单元格,选择最后一行的第一个单元格,但!在上一次选择的列最后一行中的单元格被编辑,而不是第一个单元格!

这是它的外观:
1)我选择一些细胞:
_http://img13.imageshack.us/img13/2528/beforeadd.gif

2)我按添加按钮后:
_http://img211.imageshack.us/img211/847/afteradd.gif

在此先感谢您。

PS。为什么我不能添加图像?取而代之的

cell.Selected = true; 

回答

3

尝试

CurrentCell = cell; 
相关问题