C# 将焦点设置到数据网格视图文本框列单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15918309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set focus to Data Grid View Text Box Column Cell
提问by Mohemmad K
I have a gridview of type datagridview text box column, in that following columns are there:
我有一个类型为 datagridview 文本框列的 gridview,其中有以下列:
SrNo | Description | HSNCode | Qty | Rate | Amount
I am generating amount in my program automatically, but I want to check if the user has entered to amount field without entering data in "Rate" then I want to set focus back to the "Rate" field in my program:
我在我的程序中自动生成金额,但我想检查用户是否在没有在“费率”中输入数据的情况下进入了金额字段,然后我想将焦点设置回我程序中的“费率”字段:
I have tried following code:
我试过以下代码:
private void grdData_CellLeave(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 4)
{
if(grdData.Rows[e.RowIndex].Cells[4].Value== null)
{
grdData.CurrentCell = grdData.Rows[e.RowIndex].Cells[4];
}
}
}
But the code is not working.
What should I do to switch focus to the field that is previous to the "Amount"?
Please help.
但是代码不起作用。
我应该怎么做才能将焦点切换到“金额”之前的字段?
请帮忙。
采纳答案by Praveen Nambiar
Try:
尝试:
private void grdData_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == 5)
{
if(grdData.Rows[e.RowIndex].Cells[3].Value.Equals(""))
{
grdData.ClearSelection();
grdData.Rows[e.RowIndex].Cells[3].Selected = true;
}
}
}
Update - tested and working fine using cellclick
event
更新 - 使用cellclick
事件测试并正常工作
private void grdData_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 5)
{
if(grdData.Rows[e.RowIndex].Cells[3].Value.Equals(""))
{
grdData.ClearSelection();
grdData.Rows[e.RowIndex].Cells[3].Selected = true;
}
}
}
回答by Darshan
You can try this piece of code
你可以试试这段代码
dgv.ClearSelection();
dgv.Rows[rowindex].Cells[columnindex].Selected = true;
回答by Freelancer
Refer following code:
参考以下代码:
DataGridView1.CurrentCell = dataGridView1[1, 1].Value;
'or
DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)
dataGridView1.BeginEdit(true)
For more assistance, you can follow discussion in following link:
如需更多帮助,您可以关注以下链接中的讨论:
http://www.vbdotnetforums.com/winforms-grids/11313-setting-cell-focus-datagridview.html
http://www.vbdotnetforums.com/winforms-grids/11313-setting-cell-focus-datagridview.html
Hope its helpful.
希望它有帮助。
回答by jaivir
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;
}
}
}