代码之家  ›  专栏  ›  技术社区  ›  Mohemmad K

将焦点设置为数据网格视图文本框列单元格

  •  1
  • Mohemmad K  · 技术社区  · 12 年前

    我有一个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 回复  |  直到 12 年前
        1
  •  1
  •   Praveen Nambiar    12 年前

    尝试:

    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;
           }
       }
    }
    
        2
  •  1
  •   Baby Groot Duleendra    11 年前
     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;
                    }
                }
            }
    
        3
  •  0
  •   Darshan    12 年前

    你可以试试这段代码

    dgv.ClearSelection();
    dgv.Rows[rowindex].Cells[columnindex].Selected = true;  
    
        4
  •  0
  •   Freelancer    12 年前

    请参阅以下代码:

    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

    希望它能有所帮助。