Load and Save DataGridView

Use this board to post your code snippets - tips and tricks
7 posts Page 1 of 1
Contributors
User avatar
bisnes_niko
Serious Programmer
Serious Programmer
Posts: 409
Joined: Tue Aug 24, 2010 1:21 pm

Load and Save DataGridView
bisnes_niko
Hello

Another quick snip for VB .NET, this time for DataGridView. Translated from my previous snippet.

I deicided to do this, because I find DataGridView much better than ListView for my uses. It's a slightly harder to use, but hey, thats not a problem.

Codes:

Save DataGridView
Code: Select all
 Public Shared Sub SaveDGVItems(ByVal strFilePath As String, ByVal DataGridView As DataGridView)
        Dim SaveData As String = ""

        Const Delimiter As String = "|"

        For a = 0 To DataGridView.Columns.Count - 1
            SaveData += DataGridView.Columns(a).HeaderText & Delimiter
        Next

        SaveData = SaveData.Remove(SaveData.Length - 1, 1)

        SaveData += vbNewLine

        For b = 0 To DataGridView.Rows.Count - 1
            For c = 0 To DataGridView.Columns.Count - 1
             
                    SaveData += DataGridView.Rows.Item(b).Cells(c).Value & Delimiter

                    Next
            SaveData = SaveData.Remove(SaveData.Length - 1, 1)
          
            If Not b = DataGridView.Rows.Count - 1 Then
                SaveData += vbNewLine
            End If

        Next
        My.Computer.FileSystem.WriteAllText(strFilePath, SaveData, False, System.Text.Encoding.Default)

    End Sub
Load DataGridView
Code: Select all
 Public Shared Sub LoadDGVItems(ByVal strFilePath As String, ByVal DataGridView As DataGridView)

        Const Delimiter As String = "|"

        Dim txtData As New TextBox : txtData.Text = My.Computer.FileSystem.ReadAllText(strFilePath, System.Text.Encoding.Default)

        Dim strFileData() As String = txtData.Lines

        DataGridView.Columns.Clear()
        DataGridView.Rows.Clear()

        For I = 0 To strFileData.Length - 1
            If I = 0 Then
                For Each Column In strFileData(I).Split(Delimiter)
                    DataGridView.Columns.Add(Column, Column)
                Next
            Else
                Dim dgvRow As New DataGridViewRow
                Dim dgvCell As DataGridViewCell

                For B = 0 To strFileData(I).Split(Delimiter).Length - 1

                    If B = 0 Then
                        dgvCell = New DataGridViewTextBoxCell()
                        dgvCell.Value = strFileData(I).Split(Delimiter)(B)
                        dgvRow.Cells.Add(dgvCell)
                    Else

                        dgvCell = New DataGridViewTextBoxCell()
                        dgvCell.Value = strFileData(I).Split(Delimiter)(B)
                        dgvRow.Cells.Add(dgvCell)
                    End If
                Next
                DataGridView.Rows.Add(dgvRow)
            End If
        Next

    End Sub
Last edited by bisnes_niko on Fri Apr 13, 2012 8:24 pm, edited 2 times in total.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Load and Save DataGridView
MrAksel
Good tips, but there are some unnecessary If statements. Inside the for loops, where you have 'If c = 0 Then ...' in the Save code, and 'If B = 0 Then ...' you can remove those statements because you have the same code in both cases (before and after the 'Else' line).
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
bisnes_niko
Serious Programmer
Serious Programmer
Posts: 409
Joined: Tue Aug 24, 2010 1:21 pm

Re: Load and Save DataGridView
bisnes_niko
They are not useless... they are to define if it's the first line of the text-file, which are columns, otherwise it would just add items and not columns...

EDIT: Ofcourse you could do it before that For Loop, so just change For x = 1 To Something instead of having For x = 0 To Something... this way theres no need for For... well, it happens.

EDIT: Sorry I didn't quite understand what you meant, but that's right what you say. But I translated ListView into DataGridView code, as you can see in ListView version it's as it has to be, but in datagridview it jsut doesn't matter. End. A little mistake.
Last edited by bisnes_niko on Thu Feb 09, 2012 9:47 pm, edited 2 times in total.
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Load and Save DataGridView
MrAksel
Yes they are useless. In both cases if 'c = 0' or 'c not= 0' the same code is executed. So it is, brutally yes, but useless.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: Load and Save DataGridView
Axel
Don't whine about that, really... There is lots of code on the internet that can be done much more efficient, don't get me into this !
http://vagex.com/?ref=25000
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Load and Save DataGridView
MrAksel
Did I say anything wrong now? Im sorry then, just telling a tip that might help him and others! Don't whine about someone giving help.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Axel
Coding God
Coding God
Posts: 1928
Joined: Sun Jun 27, 2010 9:15 pm

Re: Load and Save DataGridView
Axel
You obviously don't know what whining means /endofdiscussion
http://vagex.com/?ref=25000
7 posts Page 1 of 1
Return to “Quick Snips”