1

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.

Mohemmad K
  • 809
  • 7
  • 33
  • 74

4 Answers4

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;
       }
   }
}
Praveen Nambiar
  • 4,852
  • 1
  • 22
  • 31
  • is the debugger reaching `grdData.ClearSelection()`// also try with `Value.Equals` as shown – Praveen Nambiar Apr 10 '13 at 08:50
  • 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 – Mohemmad K Apr 10 '13 at 08:57
  • 1
    then i suspect you need change the event.....use `cellvalidating` event...also change the markup to `cellvalidating` event. i have updated the answer. – Praveen Nambiar Apr 10 '13 at 08:59
  • Tried updated answer with `CellValidating()` event but not working,Sir. – Mohemmad K Apr 10 '13 at 09:05
  • Though it goes in the `if` condition and executes the statements. – Mohemmad K Apr 10 '13 at 09:06
  • post the execution of the event...r u binding the `datagridview` again somewhere??? – Praveen Nambiar Apr 10 '13 at 09:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27926/discussion-between-riyaz-kalva-and-praveen-nambiar) – Mohemmad K Apr 10 '13 at 09:10
  • 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 Apr 11 '13 at 06:06
  • I applied the same code in the `CellEnter` event for tab key. But not working. – Mohemmad K Apr 11 '13 at 06:37
  • Its definitely possible...but m not sure about which event is used...its just a matter of getting the event right. – Praveen Nambiar Apr 11 '13 at 07:34
  • If you can find the way please suggest. – Mohemmad K Apr 11 '13 at 08:05
  • 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. – Praveen Nambiar Apr 11 '13 at 10:18
  • Sir, Please do something for my [this](http://stackoverflow.com/questions/16024198/not-able-to-bind-the-grid-view-in-code-behind-desktop-application) query, if you can. @Praveen Nambiar – Mohemmad K Apr 15 '13 at 21:01
  • Sir, Will you please help me for [this](http://stackoverflow.com/questions/16230715/string-was-no-recognized-as-a-valid-date-time-error-in-desktop-application) question – Mohemmad K Apr 26 '13 at 09:53
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;
                }
            }
        }
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
jaivir
  • 15
  • 1
0

You can try this piece of code

dgv.ClearSelection();
dgv.Rows[rowindex].Cells[columnindex].Selected = true;  
Darshan
  • 535
  • 1
  • 3
  • 16
0

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

Hope its helpful.

Freelancer
  • 9,008
  • 7
  • 42
  • 81
  • I cannot find `DataGridView1.Item(1, 5)` because I am working on C# @Freelancer – Mohemmad K Apr 10 '13 at 06:24
  • Sorry Sir, but I am not finding this method in the intelligent window. – Mohemmad K Apr 10 '13 at 06:26
  • try only with >>> DataGridView1.CurrentCell = dataGridView1[1, 1].Value; and then tell me. – Freelancer Apr 10 '13 at 06:56
  • 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` – Mohemmad K Apr 10 '13 at 06:59
  • @RiyazKalva can you check first answer in this>>http://stackoverflow.com/questions/9666657/how-to-move-focus-on-next-cell-in-a-datagridview-on-enter-key-press-event – Freelancer Apr 10 '13 at 07:06