dcsimg
Results 1 to 5 of 5

Thread: [RESOLVED] DGV Save Help with current code

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Resolved [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)

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,461

    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: DGV Save Help with current code

    Thanks Kevin! That's worked perfectly

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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?

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,461

    Re: [RESOLVED] DGV Save Help with current code

    Quote Originally Posted by MacShand View Post
    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
  •  



Featured


Click Here to Expand Forum to Full Width