Set focus to Data Grid View Text Box Column Cell

22,391

Solution 1

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

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

Solution 2

 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;
                }
            }
        }
Share:
22,391
Mohemmad K
Author by

Mohemmad K

Working as a trainee for ASP.Net with C#. I am a student of M.C.A.

Updated on July 06, 2022

Comments

  • Mohemmad K
    Mohemmad K almost 2 years

    I have a gridview of type datagridview text box column, in that following columns are there:

    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.

    • Praveen Nambiar
      Praveen Nambiar about 11 years
      is the event CellLeave firing? if yes, then r u able to reach till the second if statement?
    • Mohemmad K
      Mohemmad K about 11 years
      Yes sir, I have tested and it goes to the second if statement also. But not transferring the focus on the previous cell @Praveen Nambiar
    • Praveen Nambiar
      Praveen Nambiar about 11 years
      while in debugging mode - select grdData.Rows[e.RowIndex].Cells[5].Value and press cntrl+Shift+F9 and check the value
  • Mohemmad K
    Mohemmad K about 11 years
    I cannot find DataGridView1.Item(1, 5) because I am working on C# @Freelancer
  • Mohemmad K
    Mohemmad K about 11 years
    Sorry Sir, but I am not finding this method in the intelligent window.
  • Freelancer
    Freelancer about 11 years
    try only with >>> DataGridView1.CurrentCell = dataGridView1[1, 1].Value; and then tell me.
  • Mohemmad K
    Mohemmad K about 11 years
    Ya I tried the same but not working.. My line is: grdData.CurrentCell = grdData[4, e.RowIndex].Value; It gives error like Cannot Implicitly convert 'object' to Gridviewcell
  • Freelancer
    Freelancer about 11 years
    @RiyazKalva can you check first answer in this>>stackoverflow.com/questions/9666657/…
  • Praveen Nambiar
    Praveen Nambiar about 11 years
    is the debugger reaching grdData.ClearSelection()// also try with Value.Equals as shown
  • Mohemmad K
    Mohemmad K about 11 years
    Yes Sir, It is reaching to the grdData.ClearSection() statement. and also tried the condition as per you mentioned. One another problem is that when I enter the value in the Rate field then if statement is also executed. That means weather or not the values is entered in the Rate field condition is executed.. @Praveen Nambiar
  • Praveen Nambiar
    Praveen Nambiar about 11 years
    then i suspect you need change the event.....use cellvalidating event...also change the markup to cellvalidating event. i have updated the answer.
  • Mohemmad K
    Mohemmad K about 11 years
    Tried updated answer with CellValidating() event but not working,Sir.
  • Mohemmad K
    Mohemmad K about 11 years
    Though it goes in the if condition and executes the statements.
  • Praveen Nambiar
    Praveen Nambiar about 11 years
    post the execution of the event...r u binding the datagridview again somewhere???
  • Mohemmad K
    Mohemmad K about 11 years
  • Mohemmad K
    Mohemmad K about 11 years
    Sir, its working fine. But what if I move to that cell("Amount") using the "tab" key..?? Will it be possible to do same?? @Praveen Nambiar
  • Mohemmad K
    Mohemmad K about 11 years
    I applied the same code in the CellEnter event for tab key. But not working.
  • Praveen Nambiar
    Praveen Nambiar about 11 years
    Its definitely possible...but m not sure about which event is used...its just a matter of getting the event right.
  • Mohemmad K
    Mohemmad K about 11 years
    If you can find the way please suggest.
  • Praveen Nambiar
    Praveen Nambiar about 11 years
    alright...i will need some time as equipped with office work.....meanwhile...you can post another question on it about the tabkey event...so that others can also work upon it.
  • Mohemmad K
    Mohemmad K about 11 years
    Sir, Please do something for my this query, if you can. @Praveen Nambiar
  • Mohemmad K
    Mohemmad K about 11 years
    Sir, Will you please help me for this question