Solved

Datagrid datasource to table

Posted on 2012-03-12
1
203 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
ID: 37709158
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 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

12 Experts available now in Live!

Get 1:1 Help Now