Results 1 to 10 of 10

Thread: [RESOLVED] Datagridview values create folder names

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Resolved [RESOLVED] Datagridview values create folder names

    Hello can someone help me out how to create from datagridview values in rows in column 0 are the names that i want to create folder

    Example:
    Datagridview1 has 2 columns :

    NameFolder NameNumber

    so i insert a few records like

    Folder1 FolderID
    Folder2 FolderID2
    Folder3 FolderID3

    how i can make when press button to get informations from column 0 (NameFolder) and create folder in specific place like example C:\Test\ and inside will create folder with the names (Folder1,Folder2,Folder3) if not exists if exist will return back or overwrite
    any ideas how can be done?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview values create folder names

    You're asking multiple unrelated questions here. Stick to one thing at a time. Getting data from a DataGridView is the same no matter what you want to do with it, so creating folders is irrelevant to that. Creating folders is the same no matter where the paths come from, so DataGridViews are irrelevant to that. ALWAYS break your task down into the smallest parts possible and address each part individually.

    Are you able to read data from a DataGridView? What have you tried and where are you stuck in doing that specifically?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Datagridview values create folder names

    okay, how to make to read from datagridview column0 the rows information and create folders with that names in specific location

    Code:
    For Each row As DataRow In YourDataGridView.Rows
      Dim FolderNames = row("NameLine")
    Next  
    
    Dim AllFolders = (From row As DataGridViewRow
                   In YourDataGridView.Rows
                   Select row.Cells("NameLine").Value).ToList()
    or maybe would be better to put them in list then from list to create the folders?
    Code:
    Dim cellValues As New List(Of String)
         
    For Each row As DataGridViewRow In DataGridView1.Rows
        cellValues.Add(row.Cells(0).Value.ToString())
    Next
    
     Dim PathFind = "C:\Test\"
     For Each row As String In cellValues 
            If Not System.IO.Directory.Exists(PathFind+row) Then
            System.IO.Directory.CreateDirectory(PathFind+row)
           End If
        Next
    I found some code similar but its not okay
    Last edited by luckydead; Sep 24th, 2022 at 10:23 AM.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview values create folder names

    What's wrong with using a loop? Don't try to copy LINq code that you don't understand. Presumably you know the baqsics of writing a loop. Put some thought into the logic instead of expecting to find turnkey solutions to copy and paste.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Datagridview values create folder names

    edit my previous post, maybe with list would be better?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview values create folder names

    So are you able to populate the list with the data you want or not?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Datagridview values create folder names

    i was just thinking what would be the best method to get the information from the datagridview1 column 0 rows and use the rows information to create the folders with that information text.
    So i was wondering is it better to fetch all rows in column0 in list and then from the list to create folders with the stored information. Thats why i ask for assistance about this how can i start or build it correctly

    maybe would be like this:
    Code:
    Dim cellValues As New List(Of String)
         
    For Each row As DataGridViewRow In DataGridView1.Rows
        cellValues.Add(row.Cells(0).Value.ToString())
    Next
    
     Dim PathFind = "C:\Test\"
     For Each row As String In cellValues 
            If Not System.IO.Directory.Exists(PathFind+row) Then
            System.IO.Directory.CreateDirectory(PathFind+row)
           End If
        Next
    Last edited by luckydead; Sep 24th, 2022 at 10:24 AM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagridview values create folder names

    Put some thought into it. This is your application, not ours. If you are going to use a loop to get the data from the grid into a list and then use another loop to get the data from the list to create the folders and then never use the list again, what would be the point of that? Two loops and a list compared to one loop and no list. What exactly are you confused about? If you want to be able to use the list for something else later then that's a different matter but you have made no indicationb that that is the case.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Datagridview values create folder names

    Quote Originally Posted by jmcilhinney View Post
    Put some thought into it. This is your application, not ours. If you are going to use a loop to get the data from the grid into a list and then use another loop to get the data from the list to create the folders and then never use the list again, what would be the point of that? Two loops and a list compared to one loop and no list. What exactly are you confused about? If you want to be able to use the list for something else later then that's a different matter but you have made no indicationb that that is the case.
    Yes i plan to use it more than 1 time, example later i would like to fetch what was created (so possible i need to store them in a file this way right? )

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2017
    Posts
    199

    Re: Datagridview values create folder names

    okay here is what i created until now to store the information inside .txt file
    Code:
    Using writer As New StreamWriter(My.Application.Info.DirectoryPath & "\Test\Info.txt")
                For i As Integer = 0 To DataGridView1.Rows.Count - 2 Step +1
                    For j As Integer = 0 To DataGridView1.Columns.Count - 1 Step +1
                        If j = DataGridView1.Columns.Count - 1 Then
                            writer.Write(DataGridView1.Rows(i).Cells(j).Value.ToString())
                        Else
                            writer.Write(DataGridView1.Rows(i).Cells(j).Value.ToString() & "|")
                        End If
                    Next j
                    writer.WriteLine("")
                Next i
                MessageBox.Show("Data Exported")
            End Using
    So this way i make Export / Import options.
    Now how can i make from the datagridview to create the folder names depending the column0 values string names ?

    i created this method for create the folders:
    Code:
    For Each r As DataGridViewRow In DataGridView1.Rows
                If Not Directory.Exists(My.Application.Info.DirectoryPath & "\Test\" + r.Cells(0).Value.ToString) Then
                    Directory.CreateDirectory(My.Application.Info.DirectoryPath & "\Test\" + r.Cells(0).Value.ToString)
                End If
            Next
    here is the error that i receive, but folders are created:
    Code:
    System.NullReferenceException: 'Object reference not set to an instance of an object.'
    System.Windows.Forms.DataGridViewCell.Value.get returned Nothing.
    Edit: figured it out i just added (0).Value?.ToString fixed the error
    Last edited by luckydead; Sep 24th, 2022 at 12:30 PM.

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
  •  



Click Here to Expand Forum to Full Width