DataGridView - Focus a specific cell

后端 未结 10 1401
陌清茗
陌清茗 2020-12-03 00:54

How to set focus on any specified cell in DataGridView? I was expecting a simple way like Focus(rowindex,columnindex) but it is not that easy.

相关标签:
10条回答
  • 2020-12-03 01:03

    You can try this for DataGrid:

    DataGridCellInfo cellInfo = new DataGridCellInfo(myDataGrid.Items[colRow], myDataGrid.Columns[colNum]);
    DataGridCell cellToFocus = (DataGridCell)cellInfo.Column.GetCellContent(cellInfo.Item).Parent;
    ViewControlHelper.SetFocus(cellToFocus, e);
    
    0 讨论(0)
  • 2020-12-03 01:06

    in event form_load (object sender, EventArgs e) try this

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

    this code make focus on last row and 1st cell

    0 讨论(0)
  • 2020-12-03 01:07
     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;
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-03 01:08

    Set the Current Cell like:

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

    or

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

    and you can directly focus with Editing by:

    dataGridView1.BeginEdit(true)
    
    0 讨论(0)
  • 2020-12-03 01:10

    the problem with datagridview is that it select the first row automatically so you want to clear the selection by

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

    other wise it will not work

    0 讨论(0)
  • 2020-12-03 01:16

    I had a similar problem. I've hidden some columns and afterwards I tried to select the first row. This didn't really work:

    datagridview1.Rows[0].Selected = true;
    

    So I tried selecting cell[0,0], but it also didn't work, because this cell was not displayed. Now my final solution is working very well:

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

    So this selects the complete first row.

    0 讨论(0)
提交回复
热议问题