Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows WinForm
Hi,
 
Below is the code to set focus to a particular cell in gridview, but its not working.
 
dtEmployeeDocumentDetails = objEmployeeMasterBusiness.GetAllEmployeeDocumentDetails();
DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
gvEmpDocDetails.Columns.Add(chk);
chk.HeaderText = "Select";
chk.Name = "chkselect";
gvEmpDocDetails.AllowUserToAddRows = false;
gvEmpDocDetails.DataSource = dtEmployeeDocumentDetails;
DataGridViewButtonColumn btn = new DataGridViewButtonColumn();
btn.Name = "btnDeleteEmpDocument";
btn.HeaderText = "Delete";
gvEmpDocDetails.Columns.Add(btn);
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.
 
Thanks.
Posted 30-Dec-12 21:21pm
Rate this: bad
good
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:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionmode.aspx[^],
http://msdn.microsoft.com/en-us/library/3c89df86.aspx[^].
 
You will need System.Windows.Forms.DataGridViewSelectionMode.CellSelect.
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

There is another simple method to select the cell. by setting the dataGridView.CurrentCell.
eg;
 this.dataGridView1.CurrentCell = this.dataGridView1.Rows[1].Cells[2];
  Permalink  
Comments
Prathap Gangireddy at 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 at 31-Dec-12 4:16am
   
was it called after all the rows are loaded to the grid?
jibesh at 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
0 Sergey Alexandrovich Kryukov 485
1 OriginalGriff 310
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 200
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web03 | 2.8.1411019.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
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