Solved

Datagrid datasource to table

Posted on 2012-03-12
1
210 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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 …
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

636 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