Click here to Skip to main content
11,935,031 members (55,656 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Windows

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 21:21pm
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.

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 | Mobile
Web02 | 2.8.151126.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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