簡體   English   中英

DataGridView-聚焦特定的單元格

[英]DataGridView - Focus a specific cell

如何在DataGridView中的任何指定單元格上設置焦點? 我期待像Focus(rowindex,columnindex)這樣的簡單方法,但並不是那么容易。

將當前單元格設置為:

DataGridView1.CurrentCell = DataGridView1.Rows[rowindex].Cells[columnindex]

要么

DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)

您可以直接通過以下方式進行編輯:

dataGridView1.BeginEdit(true)

您可以通過將Selected屬性設置為true來將Focus設置為特定的Cell

dataGridView1.Rows[rowindex].Cells[columnindex].Selected = true;

避免設置多個選擇

dataGridView1.MultiSelect = false;

datagridview的問題在於它會自動選擇第一行,因此您想通過以下方式清除選擇

grvPackingList.ClearSelection();
dataGridView1.Rows[rowindex].Cells[columnindex].Selected = true;  

否則它將無法正常工作

我有一個類似的問題。 我隱藏了一些列,然后嘗試選擇第一行。 這實際上沒有用:

datagridview1.Rows[0].Selected = true;

所以我嘗試選擇cell[0,0] ,但是它也沒有用,因為沒有顯示該單元格。 現在我的最終解決方案運行良好:

datagridview1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;    
datagridview1.CurrentCell = datagridview1.FirstDisplayedCell;

因此,這將選擇完整的第一行。

public void M(){ 
  dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
  dataGridView1.CurrentCell.Selected = true; 
  dataGridView1.BeginEdit(true);
}

在事件form_load(對象發送者,EventArgs e)中嘗試此操作

dataGridView1.CurrentCell = dataGridView1.Rows [dataGridView1.Rows.Count1] .Cells [0];

該代碼着重於最后一行和第一個單元格

            //For me it's the best way to look for the value of a spezific column
            int seekValue = 5;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                var columnValue = Convert.ToInt32(row.Cells["ColumnName"].Value);
                if (columnValue == seekValue)
                {
                    dataGridView1.CurrentCell = row.Cells[0];
                }
            }
 private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        int row = e.RowIndex;
        int col = e.ColumnIndex;
        if (row < 0 || col != 3)
            return;
        if (e.FormattedValue.ToString().Equals(String.Empty))
        {
        }

        else
        {
            double quantity = 0;
            try
            {
                quantity = Convert.ToDouble(e.FormattedValue.ToString());
                if (quantity == 0)
                {
                    MessageBox.Show("The quantity can not be Zero", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    e.Cancel = true;
                    return;
                }
            }
            catch
            {
                MessageBox.Show("The quantity should be decimal value.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
                return;
            }
        }
    }

只需簡單地粘貼並在任何需要的地方傳遞Gridcolor()即可。

Private Sub Gridcolor()
    With Me.GridListAll
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .MultiSelect = False
        '.DefaultCellStyle.SelectionBackColor = Color.MediumOrchid
    End With
End Sub

暫無
暫無

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

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