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