我有一个类型为datagridview文本框列的gridview,其中包含以下列:
SrNo | Description | HSNCode | Qty | Rate | Amount
我自动在程序中生成金额,但我想检查用户是否已输入金额字段而未在“费率”中输入数据,然后我想将焦点设置回我的程序中的“费率”字段:< / p>
我尝试过以下代码:
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];
}
}
}
但代码无效 如何将焦点切换到“金额”之前的字段? 请帮忙。
答案 0 :(得分:1)
尝试:
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;
}
}
}
更新 - 使用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;
}
}
}
答案 1 :(得分:1)
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;
}
}
}
答案 2 :(得分:0)
你可以尝试这段代码
dgv.ClearSelection();
dgv.Rows[rowindex].Cells[columnindex].Selected = true;
答案 3 :(得分:0)
请参阅以下代码:
DataGridView1.CurrentCell = dataGridView1[1, 1].Value;
'or
DataGridView1.CurrentCell = DataGridView1.Item("ColumnName", 5)
dataGridView1.BeginEdit(true)
如需更多帮助,您可以按照以下链接进行讨论:
http://www.vbdotnetforums.com/winforms-grids/11313-setting-cell-focus-datagridview.html
希望它有所帮助。