|
-
Apr 10th, 2012, 06:35 PM
#1
Thread Starter
Frenzied Member
[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:
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Me.DataGridView1.Show()
Me.DataGridView1.Columns.Add("FullStreetAddress", "FullStreetAddress")
Me.DataGridView1.Columns.Add("City", "City")
Me.DataGridView1.Columns.Add("State", "State")
Me.DataGridView1.Columns.Add("Zip", "Zip")
Me.DataGridView1.Columns.Add("Bedrooms", "Beds")
Me.DataGridView1.Columns.Add("Baths", "Baths")
Me.DataGridView1.Columns.Add("BuildingSize", "BuildingSize")
Me.DataGridView1.Columns.Add("Lot Size", "LotSize")
Me.DataGridView1.Columns.Add("ListPrice", "ListPrice")
Me.DataGridView1.Columns.Add("ListingDate", "ListDate")
Me.DataGridView1.Columns.Add("ListingType", "ListType")
Me.DataGridView1.Columns.Add("Latitude", "Latitude")
Me.DataGridView1.Columns.Add("Longitude", "Longitude")
Me.DataGridView1.Columns.Add("GLAMax", "GLAMax")
With Me.DataGridView1.Rows
Dim row1 As String() = {Me.DataGridView1.Columns(0).Name, }
End With
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim dtlist As New DataTable()
dtlist = TryCast(DataGridView1.DataSource, DataTable)
End Sub
Public Shared Function DataGridViewToDataTable(ByVal dtg As DataGridView) As DataTable
Try
Dim dtlist As New DataTable()
Dim row As DataRow
Dim TotalDatagridviewcolumns As Integer = dtg.ColumnCount - 1
For Each c As DataGridViewColumn In dtg.Columns
Dim idColumn As DataColumn = New DataColumn()
idColumn.ColumnName = c.Name
dtlist.Columns.Add(idColumn)
Next
For Each dr As DataGridViewRow In dtg.Rows
row = dtlist.NewRow
For cn As Integer = 0 To TotalDatagridviewcolumns
row.Item(cn) = (dr.Cells(cn).Value)
Next
dtlist.Rows.Add(row)
Next
Return dtlist
Catch ex As Exception
Return Nothing
End Try
End Function
-
Apr 10th, 2012, 07:50 PM
#2
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?
-
Apr 10th, 2012, 08:40 PM
#3
Thread Starter
Frenzied Member
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:
Me.DataGridView1.Columns.Add("FullStreetAddress", "FullStreetAddress")
Me.DataGridView1.Columns.Add("City", "City")
Me.DataGridView1.Columns.Add("State", "State")
Me.DataGridView1.Columns.Add("Zip", "Zip")
Me.DataGridView1.Columns.Add("Bedrooms", "Beds")
Me.DataGridView1.Columns.Add("Baths", "Baths")
Me.DataGridView1.Columns.Add("BuildingSize", "BuildingSize")
Me.DataGridView1.Columns.Add("Lot Size", "LotSize")
Me.DataGridView1.Columns.Add("ListPrice", "ListPrice")
Me.DataGridView1.Columns.Add("ListingDate", "ListDate")
Me.DataGridView1.Columns.Add("ListingType", "ListType")
Me.DataGridView1.Columns.Add("Latitude", "Latitude")
Me.DataGridView1.Columns.Add("Longitude", "Longitude")
Me.DataGridView1.Columns.Add("GLAMax", "GLAMax")
Me.DataGridView1.DataSource = Me.BindingSource1
Me.BindingSource1.DataSource = dtlist
-
Apr 10th, 2012, 09:03 PM
#4
Thread Starter
Frenzied Member
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:
With dtlist
.Columns.Add("FullStreetAddress", GetType(String))
.Columns.Add("City", GetType(String))
.Columns.Add("State", GetType(String))
.Columns.Add("Zip", GetType(String))
.Columns.Add("Bedrooms", GetType(Integer))
.Columns.Add("Baths", GetType(Integer))
.Columns.Add("BuildingSize", GetType(Integer))
.Columns.Add("LotSize", GetType(Double))
.Columns.Add("ListPrice", GetType(Double))
.Columns.Add("ListingDate", GetType(Date))
.Columns.Add("ListingType", GetType(String))
.Columns.Add("Latitude", GetType(Decimal))
.Columns.Add("Longitude", GetType(Decimal))
.Columns.Add("GLAMax", GetType(Double))
End With
Me.DataGridView1.DataSource = dtlist
-
Apr 10th, 2012, 10:34 PM
#5
Re: User Created Unbound DGV
 Originally Posted by billboy
Ok I think I figured it out
I need to create the table first then bind to DGV
yep.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|