繁体   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