When i load the file into the datagrid, i’m getting a error message, FormatException Error.
Here, what i’m trying to do, comparing the two cell vlaues, if the col index 2 value is greater than col index 3, then show a error message.
When iload the file for the first time, i have no issues. I fi load for the seconfd time, i'm gettign the above error message.
I tried using Convert.ToInt32, int, still i’m getting the same error message. How can i fix this??
private void datagridview_CellValidating(object sender, CellValidatingEventArgs e)
{
if (e.ColumnIndex != 0)
{
if (e.RowIndex >= 0 && e.RowIndex < 8)
{
if (e.Value != null && datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value != null)
{
//Convert.ToInt32, int////
if (Double.Parse(e.Value.ToString()) <=
Double.Parse(datagridview.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString()))
{
MessageBox.Show("error");
e.Cancel = true;
datagridview.Rows[e.RowIndex].ErrorText = errorMesssage;
}
}
}
}
}
I think you should revisit your condition here
Now what would happen when you are validating for
Column1the comparison would be with the value available inColumn2andColumn1(not as what you mentioned), so if the Value inColumn1cannot be parsed then it would pop up a format exception since it cannot be parsed.So maybe you need to specify the columns in your if clause
if (e.ColumnIndex != 0) // here you allow for Column3, so that e.ColumnIndex - 1 is 2