【问题标题】:DataGridView: How to select first cell in current row when MultiSelect is trueDataGridView:当MultiSelect为真时如何选择当前行中的第一个单元格
【发布时间】:2010-10-21 17:24:57
【问题描述】:

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

        /// <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。为什么我不能添加图片?

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    而不是

    cell.Selected = true;
    

    试试

    CurrentCell = cell;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-04
      • 2012-01-13
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多