Results 1 to 3 of 3

Thread: Add Row DataGridView

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    Add Row DataGridView

    I have a DataGridView item on my form, and when I add an item to it, it doesn't add an extra row to add new items.

    Here is the code I use to load a file dialog box:
    Code:
        Private Sub Loader_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Loader.Click
            If loadFile.ShowDialog Then
                loadData(loadFile.FileName)
            End If
        End Sub
    Once a file is chosen, I use the following to add it to the grid:
    Code:
        Private Sub loadData(ByVal fileLocation As String)
            Dim fileName = GetFileNameWithoutExtension(fileLocation)
            Dim outputLocation = System.IO.Path.GetDirectoryName(fileLocation)
            Dim rowCount = Files.RowCount
            Dim good = True
            Dim i = rowCount - 1
            Files.Item(1, i).Value = fileLocation
            Files.Item(2, i).Value = outputLocation
            Files.Item(3, i).Value = fileName
        End Sub
    It loads it to the gird, and doesn't automatically add a second empty row.
    The only way for me to add another row, is by typing into one of the fields in the column, and it adds the next row for me.

    How can I get it to add the other row without me having to type into the field?

    If that doesn't make sense, please let me know!

    Thanks for the Help!
    I am still very new to VB.NET, so I have MANY questions

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Add Row DataGridView

    Of course it will do that since you have
    Code:
    Dim i = rowCount - 1
    Which is the index of the last row on the DGV. If you want to add a new row, just do this
    Code:
     Private Sub loadData(ByVal fileLocation As String)
            Dim fileName = GetFileNameWithoutExtension(fileLocation)
            Dim outputLocation = System.IO.Path.GetDirectoryName(fileLocation)
            Dim good = True
            Files.Rows.Add(Nothing, fileLocation, outputLocation, fileName)
    End Sub
    And BTW, what's the purpose of declaring the boolean variable good if you're not using it anywhere?
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    284

    Re: Add Row DataGridView

    Thanks, that works!
    I am still very new to VB.NET, so I have MANY questions

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