I have a datagridview which holds an imported excel file. I also have a MySQL database which holds the values i want to replace in my datagridview and the values that will replace the previous values.
This is my database:
What I want to accomplish is to loop inside the datagridview cells and find the values inside the Content field and replace it with the corresponding value in the Convert field. For example all 1 will be replaced by 11, all Name 1 will be replaced by Std Name 1, all 18 will be replaced by 18 Years Old.
I use the following code:
MysqlQuery = "SELECT * FROM contents"
MysqlComm = New MySqlCommand(MysqlQuery, MysqlConn)
MysqlReader = MysqlComm.ExecuteReader
While MysqlReader.Read
Dim content = MysqlReader.GetString("Content")
Dim convert = MysqlReader.GetString("Convert")
For Each row As DataGridViewRow In dgvFile.Rows
For Each cell As DataGridViewCell In row.Cells
If cell.Value IsNot Nothing Then
If cell.Value.ToString = content.ToString Then
cell.Value = convert.ToString
End If
End If
Next
Next
End While
MessageBox.Show("File successfully converted")
This code converts 1 = 11 and Name 1 = Std Name 1 but not 18 = 18 Years old and it shows this error.
QUESTION Why do I get this error and how can I fix this? Answers and suggestions are well appreciated. Thanks. :)
UPDATE For those who can't see the error message, here it is.
The following exception occurred in the DataGridView:
System.Exception: 18 Years Old is not a valid value for Double, --->
System.FormatException: Input string was not in a correct format.
at System.Number.PraseDouble(String value, NumberStyles options,
NumberFormatInfo numfmt)
at System.Double.Parse(String s, NumberStyles style, IFormatPRovider provider)
at System.ComponentModel.DoubleConverter.FromString(String value, NumberFormatInfo formatInfo)
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
--- End of inner exception stack trace ---
at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertFrom(Object value)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)
To replace this default dialog please handle the DataError event
Seems that's the line causing you the error:
cell.Value = convert.ToString
It is probably because the column type is <> String
(probably integer or double).
You can either put the value in its right type, instead of doing ToString
when you set the value.
Another option is to make the all column types of the grid String
. But than you'd have to take in account that the sort of the column would have to be changed (you will need to find the sort event and convert the value to the right type when you sort).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments