Click here to Skip to main content
13,593,032 members
Rate this:
Please Sign up or sign in to vote.
See more:

Below is the code to set focus to a particular cell in gridview, but its not working.

dtEmployeeDocumentDetails = objEmployeeMasterBusiness.GetAllEmployeeDocumentDetails();
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
chk.HeaderText = "Select";
chk.Name = "chkselect";
gvEmpDocDetails.AllowUserToAddRows = false;
gvEmpDocDetails.DataSource = dtEmployeeDocumentDetails;
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.Name = "btnDeleteEmpDocument";
btn.HeaderText = "Delete";
gvEmpDocDetails.Columns[1].Visible = false;
gvEmpDocDetails.Columns[4].Visible = false;
gvEmpDocDetails.Columns[5].Visible = false;
gvEmpDocDetails.Columns[6].Visible = false;
gvEmpDocDetails.Columns[8].Visible = false;
gvEmpDocDetails.Columns[12].Visible = false;
gvEmpDocDetails.Rows[2].Cells[0].Selected = true;

Can you please let me know how to set focus to a cell so that the code written in my cell click event works fine when formloads.

Posted 30-Dec-12 20:21pm
Updated 19-Aug-17 6:58am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

First of all, there is no a focus on a cell. Focus is always a keyboard focus; this is the attribute of a control, not any of its parts. What you need is selection, which is not related to focus.

Also, the result of selection depends on selection style of your DataGridView. Please see DataGridViewSelectionMode:[^],[^].

You will need System.Windows.Forms.DataGridViewSelectionMode.CellSelect.

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

There is another simple method to select the cell. by setting the dataGridView.CurrentCell.
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[1].Cells[2];
Prathap Gangireddy 31-Dec-12 3:40am
Hi Jibesh,

I tried in the Form load event...but its not working..still the Firstrow ..FirstColumn cell is selected..
Jibesh 31-Dec-12 4:16am
was it called after all the rows are loaded to the grid?
Jibesh 31-Dec-12 5:29am
I tried the same code and it works fine. I called this method in Form_Load event after assigning the dataTable as datasource to dgv.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

 Grid1.CurrentCell = Grid1.Rows(1).Cells(3)
CHill60 20-Aug-17 7:41am
The question was asked and answered over 4 years ago. You have added nothing new to this thread

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02-2016 | 2.8.180618.1 | Last Updated 19 Aug 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100