Solved

Datagrid datasource to table

Posted on 2012-03-12
1
198 Views
Last Modified: 2012-03-12
The following code creates my datagrid.  Then after the user fills out the data grid i want it to go to a table.  To accomplish that i use this
Dim dtnew As New DataTable
            dtnew = dvdataview.Table      
the problem is that the two columns that i add manually (LocationName,AccountName) dont come over.



     If Not lbCompanies.SelectedIndex < 0 Or Not lbDepartmentName.SelectedIndex < 0 Then
                dsadditems = New DataSet
                Dim DT As New DataTable
                With DT.Columns
                    .Add("ItemNumber")
                    .Add("ItemPack")
                    .Add("ItemSize")
                    .Add("ItemDescription")
                    .Add("ItemBrand")
                    .Add("Manufacturer")
                    .Add("UPC")
                    .Add("VendorItemNumber")
                    .Add("ServingsPerCase")
                    .Add("UnitOfMeasure")
                    .Add("PortionCost")
                    .Add("PricePerPound")
                    .Add("CaseCost")
                    .Add("UnitCost")
                End With
                dsadditems.Tables.Add(DT)
                dgadditem.DataSource = dsadditems.Tables(0)
                dvdataview = New DataView(dgadditem.DataSource)
                dgadditem.DataSource = dvdataview
                '' add account name drop box
                Dim accountnamecmb = New DataGridViewComboBoxColumn()
                accountnamecmb.HeaderText = "Account Name"
                accountnamecmb.Name = "AccountName"
                accountnamecmb.MaxDropDownItems = 4
                accountnamecmb.Items.Add("Food Purchases")
                accountnamecmb.Items.Add("Paper & Chemical")
                accountnamecmb.Items.Add("Equipment & Furniture")
                accountnamecmb.Items.Add("Smallwares")
                dgadditem.Columns.Add(accountnamecmb)
                'add a location drop box
                Dim ds As New DataSet
                Dim datest As New OleDb.OleDbDataAdapter("Select ID,departmentcode from departments where department = '" & lbDepartmentName.SelectedItem.ToString & "'", mainform.dbconnection)
                datest.Fill(ds)
                Dim departmentID As Integer = ds.Tables(0).Rows(0).Item(0)
                ds = New DataSet
                datest = New OleDb.OleDbDataAdapter("Select location from locations where departmentID = " & departmentID, mainform.dbconnection)
                datest.Fill(ds)
                Dim jmb As New DataGridViewComboBoxColumn()
                jmb.HeaderText = "Location Name"
                jmb.Name = "LocationName"
                jmb.MaxDropDownItems = ds.Tables(0).Rows.Count
                For Each dr As DataRow In ds.Tables(0).Rows
                    jmb.Items.Add(dr.Item(0))
                Next
                dgadditem.Columns.Add(jmb)


            Else
                MsgBox("You need to select both a company and a department.")
            End If
0
Comment
Question by:Millkind
1 Comment
 
LVL 53

Accepted Solution

by:
Dhaest earned 500 total points
Comment Utility
I'm afraid that you 'll need to loop over your datagrid to place all the columns into a datatable

Dim dt As DataTable, dr As DataRow

Dim DT As New DataTable
                With DT.Columns
                    .Add("ItemNumber")
                    .Add("ItemPack")
                    .Add("ItemSize")
                    .Add("ItemDescription")
                    .Add("ItemBrand")
                    .Add("Manufacturer")
                    .Add("UPC")
                    .Add("VendorItemNumber")
                    .Add("ServingsPerCase")
                    .Add("UnitOfMeasure")
                    .Add("PortionCost")
                    .Add("PricePerPound")
                    .Add("CaseCost")
                    .Add("UnitCost")
                    .Add("LocationName")
                    .Add("AccountName")
                End With


For i = 0 To datagridview.Rows.Count - 2
dr = dt.NewRow
For j = 0 To datagridview.Columns.Count - 1
dr(j) = datagridview.Rows(i).Cells(j).Value.
Next
dt.Rows.Add(dr)
Next
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now