Solved

Datagrid datasource to table

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 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