How to validate if user enter an integer or not in datagridview cell in c#?

Multi tool use
How to validate if user enter an integer or not in datagridview cell in c#?
In my windows application Purchase Order Datagridview as follows
If user enter not a number value in quantity cell in the current row, it will throw an exception.I would like to know how to continue if I press ok button in this error message.I tried following code.But it will popup this message continuously.How to solve this.
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
double unitPrice = 0;
int quantity = 0;
quantity = Convert.ToInt32(row.Cells[dataGridView1.Columns[2].Index].Value);
unitPrice = Convert.ToDouble(row.Cells[dataGridView1.Columns[3].Index].Value);
row.Cells[dataGridView1.Columns[4].Index].Value = quantity * unitPrice;
}
}
catch (Exception ex)
{
MessageBox.Show(" Error " + ex.Message);
dataGridView1.CurrentRow.Cells[2].Value = DBNull.Value;
}
}
Unit price,BookName and ISBN_No loaded by using a data reader
– Kith
Jul 3 at 8:37
OK. I've posed an answer which should do what you need.
– Wheels73
Jul 3 at 8:39
please find my updated answer just copy paste my solution into ur IDE
– D-john Anshani
Jul 3 at 8:47
1 Answer
1
Please See this.use int.TryParse
private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
{
try
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
double unitPrice = 0;
int quantity = 0;
int.TryParse(row.Cells[dataGridView1.Columns["Enter Your Column Name here"].Index].Value, out quantity))
if(!(quantity > 0))
{
MessageBox.Show(" Error ");
return;
}
unitPrice = Convert.ToDouble(row.Cells[dataGridView1.Columns["Enter Your Column Name here"].Index].Value);
row.Cells[dataGridView1.Columns["Enter Your Column Name here"].Index].Value = quantity * unitPrice;
}
}
catch (Exception ex)
{
}
}
Full Running Example to understand Tryparse:-
class Program
{
static void Main(string args)
{
string abc = "10abc";
int result = 0;
int.TryParse(abc, out result);
Console.WriteLine(result);
Console.ReadKey();
}
}
How I have tested:-
class Program
{
static void Main(string args)
{
string abc = "10abc";
int result = 0;
int.TryParse(abc, out result);
if(!(result>0))
{
Console.Write("Enter Numeric Value");
}
Console.ReadKey();
}
}
Your code doesn't compile? TryParse needs an out parameter?? This also doesn't suppress the exception that OP had requested.
– Wheels73
Jul 3 at 8:18
Please find my updated Answer tested now
– D-john Anshani
Jul 3 at 8:24
Error occurred in tryparse block.It said "No overload for method 'TryParse' takes1 arguments"
– Kith
Jul 3 at 8:33
its two now int.TryParse(row.Cells[dataGridView1.Columns[2].Index].Value, out quantity))
– D-john Anshani
Jul 3 at 8:35
Please try and let me know @Kith
– D-john Anshani
Jul 3 at 8:38
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How are you binding the data to the grid? Have you added the columns manually?
– Wheels73
Jul 3 at 8:00