Results 1 to 5 of 5

Thread: [RESOLVED] User Created Unbound DGV

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Resolved [RESOLVED] User Created Unbound DGV

    Hi guys I am stuck on have a user enter data into a DGV and then creating a table out of that data. I need to know how to take the data the user enters into a DGV cell and have addded to the dgv so that it can then be added the a table here is what i got so far:


    code Code:
    1. Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.         Me.DataGridView1.Show()
    3.         Me.DataGridView1.Columns.Add("FullStreetAddress", "FullStreetAddress")
    4.         Me.DataGridView1.Columns.Add("City", "City")
    5.         Me.DataGridView1.Columns.Add("State", "State")
    6.         Me.DataGridView1.Columns.Add("Zip", "Zip")
    7.         Me.DataGridView1.Columns.Add("Bedrooms", "Beds")
    8.         Me.DataGridView1.Columns.Add("Baths", "Baths")
    9.         Me.DataGridView1.Columns.Add("BuildingSize", "BuildingSize")
    10.         Me.DataGridView1.Columns.Add("Lot Size", "LotSize")
    11.         Me.DataGridView1.Columns.Add("ListPrice", "ListPrice")
    12.         Me.DataGridView1.Columns.Add("ListingDate", "ListDate")
    13.         Me.DataGridView1.Columns.Add("ListingType", "ListType")
    14.         Me.DataGridView1.Columns.Add("Latitude", "Latitude")
    15.         Me.DataGridView1.Columns.Add("Longitude", "Longitude")
    16.         Me.DataGridView1.Columns.Add("GLAMax", "GLAMax")
    17.  
    18.         With Me.DataGridView1.Rows
    19.             Dim row1 As String() = {Me.DataGridView1.Columns(0).Name, }
    20.  
    21.         End With
    22.     End Sub
    23.  
    24.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    25.         Dim dtlist As New DataTable()
    26.         dtlist = TryCast(DataGridView1.DataSource, DataTable)
    27.  
    28.     End Sub
    29.  
    30.     Public Shared Function DataGridViewToDataTable(ByVal dtg As DataGridView) As DataTable
    31.         Try
    32.             Dim dtlist As New DataTable()
    33.             Dim row As DataRow
    34.             Dim TotalDatagridviewcolumns As Integer = dtg.ColumnCount - 1
    35.  
    36.             For Each c As DataGridViewColumn In dtg.Columns
    37.                 Dim idColumn As DataColumn = New DataColumn()
    38.                 idColumn.ColumnName = c.Name
    39.                 dtlist.Columns.Add(idColumn)
    40.  
    41.             Next
    42.             For Each dr As DataGridViewRow In dtg.Rows
    43.                 row = dtlist.NewRow
    44.                 For cn As Integer = 0 To TotalDatagridviewcolumns
    45.                     row.Item(cn) = (dr.Cells(cn).Value)
    46.                 Next
    47.                 dtlist.Rows.Add(row)
    48.             Next
    49.  
    50.             Return dtlist
    51.         Catch ex As Exception
    52.             Return Nothing
    53.  
    54.         End Try
    55.     End Function

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

    Re: User Created Unbound DGV

    You say that the grid is unbound yet you want the data in a DataTable. Wouldn't it make more sense just to bind the DataTable to the grid and then you don't have to do anything else because whatever the user enters just goes straight into the DataTable? Why do manually what data-binding can do automatically?
    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
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: User Created Unbound DGV

    Thank John I didnt realize I could do that. How does the data the user enetered get into the table?

    I thought i saw in another post you responded too that it was entered as the user tabbed through the dgv cells and you recommened using a bindingsource? Apparently I am mistaken or not impelmenting it correctly. Me datatable is empty. I beleive I stiill need to add the row?

    vb Code:
    1. Me.DataGridView1.Columns.Add("FullStreetAddress", "FullStreetAddress")
    2.         Me.DataGridView1.Columns.Add("City", "City")
    3.         Me.DataGridView1.Columns.Add("State", "State")
    4.         Me.DataGridView1.Columns.Add("Zip", "Zip")
    5.         Me.DataGridView1.Columns.Add("Bedrooms", "Beds")
    6.         Me.DataGridView1.Columns.Add("Baths", "Baths")
    7.         Me.DataGridView1.Columns.Add("BuildingSize", "BuildingSize")
    8.         Me.DataGridView1.Columns.Add("Lot Size", "LotSize")
    9.         Me.DataGridView1.Columns.Add("ListPrice", "ListPrice")
    10.         Me.DataGridView1.Columns.Add("ListingDate", "ListDate")
    11.         Me.DataGridView1.Columns.Add("ListingType", "ListType")
    12.         Me.DataGridView1.Columns.Add("Latitude", "Latitude")
    13.         Me.DataGridView1.Columns.Add("Longitude", "Longitude")
    14.         Me.DataGridView1.Columns.Add("GLAMax", "GLAMax")
    15.  
    16.         Me.DataGridView1.DataSource = Me.BindingSource1
    17.         Me.BindingSource1.DataSource = dtlist

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Aug 2009
    Location
    Los Angeles
    Posts
    1,335

    Re: User Created Unbound DGV

    Ok I think I figured it out
    I need to create the table first then bind to DGV

    vb Code:
    1. With dtlist
    2.             .Columns.Add("FullStreetAddress", GetType(String))
    3.             .Columns.Add("City", GetType(String))
    4.             .Columns.Add("State", GetType(String))
    5.             .Columns.Add("Zip", GetType(String))
    6.             .Columns.Add("Bedrooms", GetType(Integer))
    7.             .Columns.Add("Baths", GetType(Integer))
    8.             .Columns.Add("BuildingSize", GetType(Integer))
    9.             .Columns.Add("LotSize", GetType(Double))
    10.             .Columns.Add("ListPrice", GetType(Double))
    11.             .Columns.Add("ListingDate", GetType(Date))
    12.             .Columns.Add("ListingType", GetType(String))
    13.             .Columns.Add("Latitude", GetType(Decimal))
    14.             .Columns.Add("Longitude", GetType(Decimal))
    15.             .Columns.Add("GLAMax", GetType(Double))
    16.         End With
    17.         Me.DataGridView1.DataSource = dtlist

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: User Created Unbound DGV

    Quote Originally Posted by billboy View Post
    Ok I think I figured it out
    I need to create the table first then bind to DGV
    yep.

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