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;
}
}
}
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, 2022Comments
-
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 about 11 yearsis the event
CellLeave
firing? if yes, then r u able to reach till the secondif
statement? -
Mohemmad K about 11 yearsYes 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 about 11 yearswhile in debugging mode - select
grdData.Rows[e.RowIndex].Cells[5].Value
and presscntrl+Shift+F9
and check the value
-
-
Mohemmad K about 11 yearsI cannot find
DataGridView1.Item(1, 5)
because I am working on C# @Freelancer -
Mohemmad K about 11 yearsSorry Sir, but I am not finding this method in the intelligent window.
-
Freelancer about 11 yearstry only with >>> DataGridView1.CurrentCell = dataGridView1[1, 1].Value; and then tell me.
-
Mohemmad K about 11 yearsYa I tried the same but not working.. My line is:
grdData.CurrentCell = grdData[4, e.RowIndex].Value;
It gives error likeCannot Implicitly convert 'object' to Gridviewcell
-
Freelancer about 11 years@RiyazKalva can you check first answer in this>>stackoverflow.com/questions/9666657/…
-
Praveen Nambiar about 11 yearsis the debugger reaching
grdData.ClearSelection()
// also try withValue.Equals
as shown -
Mohemmad K about 11 yearsYes 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 thenif
statement is also executed. That means weather or not the values is entered in the Rate field condition is executed.. @Praveen Nambiar -
Praveen Nambiar about 11 yearsthen i suspect you need change the event.....use
cellvalidating
event...also change the markup tocellvalidating
event. i have updated the answer. -
Mohemmad K about 11 yearsTried updated answer with
CellValidating()
event but not working,Sir. -
Mohemmad K about 11 yearsThough it goes in the
if
condition and executes the statements. -
Praveen Nambiar about 11 yearspost the execution of the event...r u binding the
datagridview
again somewhere??? -
Mohemmad K about 11 years
-
Mohemmad K about 11 yearsSir, 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 about 11 yearsI applied the same code in the
CellEnter
event for tab key. But not working. -
Praveen Nambiar about 11 yearsIts definitely possible...but m not sure about which event is used...its just a matter of getting the event right.
-
Mohemmad K about 11 yearsIf you can find the way please suggest.
-
Praveen Nambiar about 11 yearsalright...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 about 11 yearsSir, Please do something for my this query, if you can. @Praveen Nambiar
-
Mohemmad K about 11 yearsSir, Will you please help me for this question