Hello,
I have a very simple form with a DataGrid with 9 columns. Columns 1,2,3,4,6,&7 are DataGridViewTextBoxColumns, Column 5 is a DataGridViewComboBoxColumn, and Column 9 is a DataGridViewCheckBoxColumn. There is then a Load Button and a Save Button with a text field telling it what filename to save it as. (FYI it's making it into .CSV format)
I have two main problems:
First, when reading in data using the following code on a button click, if the data doesn't match exactly what's in the DataGridViewComboBoxColumn there is an error which I don't know how to "handle". I'd like to just leave the cell " " blank if it doesn't match properly but don't know where to put a statement to handle this.
Here is the click event- for the Load Button:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
Dim fName As String = ""<br />
Dim OpenFileDialog1 As New OpenFileDialog<br />
OpenFileDialog1.InitialDirectory = "C:\TEMP\"<br />
OpenFileDialog1.Filter = "CSV files (*.csv)|*.CSV"<br />
OpenFileDialog1.FilterIndex = 2<br />
OpenFileDialog1.RestoreDirectory = True<br />
If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then<br />
fName = OpenFileDialog1.FileName<br />
End If<br />
Me.TextBox1.Text = fName<br />
Dim TextLine As String = ""<br />
Dim SplitLine() As String<br />
Me.DataGridView1.Rows.Clear()<br />
<br />
If System.IO.File.Exists(fName) = True Then<br />
Dim objReader As New System.IO.StreamReader(fName)<br />
Do While objReader.Peek() <> -1<br />
TextLine = objReader.ReadLine()<br />
SplitLine = Split(TextLine, ",")<br />
Me.DataGridView1.Rows.Add(SplitLine)<br />
Loop<br />
objReader.Close()<br />
Else<br />
MsgBox("File Does Not Exist")<br />
End If<br />
End Sub
And the error message:
The following exception occurred in the DataGridView:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event.
Second, if I save the file using a different button click, it writes the DataGridViewCheckBoxColumn as TRUE if it is checked but nothing if it is not checked, then when I go to load the same file which I have saved back into the form it throws another error for any event which isn't written out as FALSE, i.e. anything that is just left blank. I could either fix this when I'm saving the file somehow or put a statement in the load button to handle it however I don't know how to do either.
Here is the code for saving:
Private Sub SaveGridDataInFile(ByRef fName As String)<br />
Dim I As Integer = 0<br />
Dim j As Integer = 0<br />
Dim cellvalue$<br />
Dim rowLine As String = ""<br />
Try<br />
Dim objWriter As New System.IO.StreamWriter(fName, True)<br />
For j = 0 To (DataGridView1.Rows.Count - 2)<br />
For I = 0 To (DataGridView1.Columns.Count - 1)<br />
If Not TypeOf DataGridView1.CurrentRow.Cells.Item(I).Value Is DBNull Then<br />
cellvalue = DataGridView1.Item(I, j).Value<br />
Else<br />
cellvalue = ""<br />
End If<br />
rowLine = rowLine + cellvalue + ","<br />
Next<br />
objWriter.WriteLine(rowLine)<br />
rowLine = ""<br />
Next<br />
objWriter.Close()<br />
MsgBox("Text written to file")<br />
Catch e As Exception<br />
MessageBox.Show("Error occured while writing to the file." + e.ToString())<br />
Finally<br />
FileClose(1)<br />
End Try<br />
End Sub<br />
<br />
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click<br />
SaveGridDataInFile(Me.TextBox1.Text)<br />
End Sub
And the error message:
The following exception occurred in the DataGridView:
System.FormatException: is not a valid value for Boolean. --> System.FormatException:
String was not recognized as a valid Boolean.
...
...
...
To replace this default dialog please handle the DataError event.
Any help would be greatly appriciated, thank you!
Elizabeth