代码之家  ›  专栏  ›  技术社区  ›  nightcoder

DataGridView:当多选为真时,如何选择当前行中的第一个单元格

  •  1
  • nightcoder  · 技术社区  · 17 年前


    我想创建一个方法来选择当前行中的第一个单元格,所以我写了这个:

            /// <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);
            }


    它看起来是这样的:

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

    PS.为什么我不能添加图片?

    1 回复  |  直到 17 年前
        1
  •  3
  •   Arnold Spence    17 年前

    而非

    cell.Selected = true;
    

    CurrentCell = cell;
    
    推荐文章