Solved

Datagrid datasource to table

Posted on 2012-03-12
1
205 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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 …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

820 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