如何在Datagridview中将焦点设置到特定单元格 [英] How to set focus to a particular cell in Datagridview
问题描述
下面是在gridview中将焦点设置到特定单元格的代码,但它不起作用。
dtEmployeeDocumentDetails = objEmployeeMasterBusiness.GetAllEmployeeDocumentDetails();
DataGridViewCheckBoxColumn CHK =新DataGridViewCheckBoxColumn();
gvEmpDocDetails.Columns.Add(CHK);
chk.HeaderText =选择;
chk.Name =chkselect;
gvEmpDocDetails.AllowUserToAddRows = false;
gvEmpDocDetails.DataSource = dtEmployeeDocumentDetails;
DataGridViewButtonColumn BTN =新DataGridViewButtonColumn();
btn.Name = btnDeleteEmpDocument;
btn.HeaderText =删除;
gvEmpDocDetails.Columns.Add(btn);
gvEmpDocDetails.Columns [1]。可见= FALSE;
gvEmpDocDetails.Columns [4]。可见= FALSE;
gvEmpDocDetails.Columns [ 5]。可见= FALSE;
gvEmpDocDetails.Columns [6]。可见= FALSE;
gvEmpDocDetails.Columns [8]。可见= FALSE;
gvEmpDocDetails.Columns [12] .Visible = false;
gvEmpDocDetails.Rows [2] .Cells [0] .Selected = true;
你能告诉我如何将焦点设置到单元格,以便在单元格单击事件中编写的代码在表单加载时工作正常。
谢谢。
首先,没有关注细胞。专注始终是键盘焦点;这是控件的属性,而不是它的任何部分。你需要的是选择,与焦点无关。
另外,选择的结果取决于DataGridView的选择风格
。请参阅DataGridViewSelectionMode
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionmode.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/3c89df86.aspx [ ^ ]。
你需要System.Windows.Forms.DataGridViewSelectionMode.CellSelect
。
-SA
还有另一种选择单元格的简单方法。通过设置dataGridView.CurrentCell。
eg;
this .dataGridView1 .CurrentCell = this .dataGridView1.Rows [ 1 ]。单元格[ 2 跨度>];
Grid1.Focus()
Grid1.CurrentCell = Grid1.Rows(1).Cells( 3)
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.
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 yourDataGridView
. Please seeDataGridViewSelectionMode
:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.selectionmode.aspx[^],
http://msdn.microsoft.com/en-us/library/3c89df86.aspx[^].
You will needSystem.Windows.Forms.DataGridViewSelectionMode.CellSelect
.
—SA
There is another simple method to select the cell. by setting the dataGridView.CurrentCell.
eg;
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[1].Cells[2];
Grid1.Focus() Grid1.CurrentCell = Grid1.Rows(1).Cells(3)
这篇关于如何在Datagridview中将焦点设置到特定单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!