1

我有一个 datagridview 文本框列类型的 gridview,其中有以下列:

SrNo    | Description    | HSNCode    | Qty   | Rate   | Amount

我在我的程序中自动生成金额,但我想检查用户是否已输入金额字段而没有在“费率”中输入数据,然后我想将焦点重新设置回程序中的“费率”字段:

我试过以下代码:

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];
       }
    }
}

但是代码不起作用。
我应该怎么做才能将焦点切换到“金额”之前的字段?
请帮忙。

4

4 回答 4

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;
                }
            }
        }
于 2013-09-05T05:52:13.420 回答
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;
       }
   }
}
于 2013-04-10T08:41:46.883 回答
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

希望它有帮助。

于 2013-04-10T06:20:56.893 回答
0

你可以试试这段代码

dgv.ClearSelection();
dgv.Rows[rowindex].Cells[columnindex].Selected = true;  
于 2013-04-10T06:18:36.903 回答