|
-
Mar 16th, 2012, 11:02 AM
#1
Thread Starter
Addicted Member
[RESOLVED] DGV Save Help with current code
Hi all
The following saves my dgv into a .txt file, comma separated and allows me to reload it later.
Only issue is with my checkbox columns as this code only saves a "true" and when the check box is "false" rather it leaves it empty in the .txt file.
eg...for a dgv of three checkboxes it would save:
True,,
When Column 1 = True and Column 2 and 3 = False
This then causes issues when I come to reopen my dgv later on.
Any advice?
Code:
Dim Rows = (From row In DataGridView1.Rows _
Where Not DirectCast(row, DataGridViewRow).IsNewRow _
Let RowItem = String.Join(",", Array _
.ConvertAll( _
DirectCast(row, DataGridViewRow).Cells.Cast(Of DataGridViewCell).ToArray, _
Function(c As DataGridViewCell) _
If(c.Value Is Nothing, "", c.Value.ToString))) _
Select RowItem).ToArray
IO.File.WriteAllLines("C:\test.txt", Rows)
-
Mar 16th, 2012, 11:34 AM
#2
Re: DGV Save Help with current code
If you set the values for the cells when adding a row along with setting default values for a new row (see below) your CheckBox columns will have True or False when exporting to text file.
Code:
''' <summary>
''' Three columns setup as CheckBox columns in the IDE
''' Column1, Column2, Column3
''' </summary>
''' <remarks></remarks>
Public Class Form2
Private Filename As String = IO.Path.Combine(Application.StartupPath, "Text.txt")
Private Sub Form2_Load( _
ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IO.File.Exists(Filename) Then
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(Filename)
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim currentRow As String()
While Not MyReader.EndOfData
currentRow = MyReader.ReadFields()
DataGridView1.Rows.Add(New Object() {currentRow(0), currentRow(1), currentRow(2)})
End While
End Using
Else
DataGridView1.Rows.Add(New Object() {True, False, False})
DataGridView1.Rows.Add(New Object() {True, False, True})
DataGridView1.Rows.Add(New Object() {True, False, False})
End If
End Sub
Private Sub Button1_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Rows = (From row In DataGridView1.Rows _
Where Not DirectCast(row, DataGridViewRow).IsNewRow _
Let RowItem = String.Join(",", Array _
.ConvertAll( _
DirectCast(row, DataGridViewRow).Cells.Cast(Of DataGridViewCell).ToArray, _
Function(c As DataGridViewCell) _
If(c.Value Is Nothing, "", c.Value.ToString))) _
Select RowItem).ToArray
IO.File.WriteAllLines(Filename, Rows)
End Sub
Private Sub DataGridView1_DefaultValuesNeeded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowEventArgs) Handles DataGridView1.DefaultValuesNeeded
With e.Row
.Cells("Column1").Value = False
.Cells("Column2").Value = False
.Cells("Column3").Value = False
End With
End Sub
End Class
-
Mar 16th, 2012, 11:39 AM
#3
Thread Starter
Addicted Member
Re: DGV Save Help with current code
Thanks Kevin! That's worked perfectly
-
Mar 16th, 2012, 12:07 PM
#4
Thread Starter
Addicted Member
Re: [RESOLVED] DGV Save Help with current code
Hi Kevin...slight problem...
I'm now having trouble with my Check Boxes...previously I had the code so that when check box in column 1 was ticked or true, then column 1 would go yellow.
It was coded under the CellValueChanged.
So because there's a default value, technically the cell value has changed, and so my cell colours yellow.
What do you suggest?
-
Mar 16th, 2012, 12:54 PM
#5
Re: [RESOLVED] DGV Save Help with current code
 Originally Posted by MacShand
Hi Kevin...slight problem...
I'm now having trouble with my Check Boxes...previously I had the code so that when check box in column 1 was ticked or true, then column 1 would go yellow.
It was coded under the CellValueChanged.
So because there's a default value, technically the cell value has changed, and so my cell colours yellow.
What do you suggest?
It depends on what your CellValueChanged code looks like. Without seeing it this is hard to suggest other than check to see if the row is a new row i.e. IsNewRow.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|