56

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.

SMUsamaShah
  • 7,677
  • 22
  • 88
  • 131
  • Try this one out..... http://stackoverflow.com/questions/20822270/canceledit-does-not-keep-focus-on-edited-cell-in-datagridview-c-sharp –  May 07 '15 at 17:15

10 Answers10

110

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)
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • 11
    .CurrentCell worked, but DataGridView1.Item(1, 5) did not. I had to do: dataGridView1.CurrentCell = dataGridView1.Rows[rowindex].Cells[columnindex]. – Samik R Sep 22 '11 at 23:17
  • 1
    @SamikR DataGridView1[colIndex, rowIndex] – B H Apr 29 '18 at 18:49
  • 2
    Take note that BeginEdit won't work from the Constructor. You should do it from one of the action functions like MainForm_Shown if you want to select something at form startup. – FoppyOmega May 04 '21 at 19:16
  • When doing this from inside a grid view handler, you might need to defer the action using `BeginInvoke`. – tm1 Jan 30 '23 at 09:53
14

you can set Focus to a specific Cell by setting Selected property to true

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

to avoid Multiple Selection just set

dataGridView1.MultiSelect = false;
Binil
  • 6,445
  • 3
  • 30
  • 40
7

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

Nighil
  • 4,099
  • 7
  • 30
  • 56
4

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.

Mickaël Derriey
  • 12,796
  • 1
  • 53
  • 57
Jules
  • 567
  • 1
  • 6
  • 15
1
public void M(){ 
  dataGridView1.CurrentCell = dataGridView1.Rows[0].Cells[0];
  dataGridView1.CurrentCell.Selected = true; 
  dataGridView1.BeginEdit(true);
}
SMUsamaShah
  • 7,677
  • 22
  • 88
  • 131
1

Just Simple Paste And Pass Gridcolor() any where You want.

Private Sub Gridcolor()
    With Me.GridListAll
        .SelectionMode = DataGridViewSelectionMode.FullRowSelect
        .MultiSelect = False
        '.DefaultCellStyle.SelectionBackColor = Color.MediumOrchid
    End With
End Sub
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • 2
    Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Nov 28 '16 at 09:56
0

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

Ahmed Soliman
  • 416
  • 5
  • 11
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];
                }
            }
-2
 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;
            }
        }
    }
Barnee
  • 3,212
  • 8
  • 41
  • 53
jaivir
  • 15
  • 1
-2

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);
BDL
  • 21,052
  • 22
  • 49
  • 55
Reb
  • 13
  • 2
  • 1
    Welcome to StackOverflow. Please provide some explanation why do you think your proposed solution might help the OP. – Peter Csala Aug 07 '20 at 12:25