DataGridView:当MultiSelect为真时,如何选择当前行中的第一个单元格 [英] DataGridView: How to select first cell in current row when MultiSelect is true

查看:104
本文介绍了DataGridView:当MultiSelect为真时,如何选择当前行中的第一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  ///< summary> 
///该方法选择当前行中的第一个可见单元格。
///如果当前行为空,则不做任何操作。
///如果选择了任何单元格,则在选择第一个单元格之前,它们将被取消选择。
///< / summary>
public void SelectFirstCellInCurrentRow()
{
if(CurrentRow == null)return;

ClearSelection();
foreach(CurrentRow.Cells中的DataGridViewCell单元格)
{
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



提前谢谢。 p>

PS。为什么我不能添加图像?

解决方案

而不是

  cell.Selected = true; 

尝试

  CurrentCell = cell; 


I am creating my DataGridViewEx class, inherited from DataGridView.
I want to create a method for selecting first cell in a current row, so I wrote this:

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

And I want to use it like this for example:

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

My task is to add a new row to the grid and start editing its first cell.
This code works fine if grid.MultiSelect = false, but if grid.MultiSelect = true, then it doesn't work as expected: all cells are deselected, first cell of the last row is selected, but !!! the cell in last row on column that was last selected gets edited, instead of first cell!

This is how it looks:
1) I select some cells:
_http://img13.imageshack.us/img13/2528/beforeadd.gif

2) After I press Add button:
_http://img211.imageshack.us/img211/847/afteradd.gif

Thank you in advance.

PS. Why can't I add images?

解决方案

Instead of

cell.Selected = true;

try

CurrentCell = cell;

这篇关于DataGridView:当MultiSelect为真时,如何选择当前行中的第一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆