簡體   English   中英

DataGridView:MultiSelect為true時如何選擇當前行中的第一個單元格

[英]DataGridView: How to select first cell in current row when MultiSelect is true

我正在創建從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。 為什么我不能添加圖像?

代替

cell.Selected = true;

嘗試

CurrentCell = cell;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM