Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Datagrid > DataTable > Excel

Datagrid > DataTable > Excel

Scheduled Pinned Locked Moved Visual Basic
database
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RichardBerry
    wrote on last edited by
    #1

    Hi I have a form with 7 Datagrids. I populate DataTables from a database. I create a Dataview of the tables, and apply various filters based on user selections. I then use the DataVeiw as the Datagrid DataSource. I have also used Tablestyles to give the Datagrid Columns 'Readable' columnnames, and have adjusted the width of certain columns and set the columnwidth to 0 on some columns in order to hide them. This all works great. Now I want to add the option to Export what displayed by the datgrids to Excel. This is easy with a Datatable, since I can use the Column Names as headings, and then use a loop For Each Col as Datacoumn in dtMyTable.Columns For each Row as datarow in dtMyTable.Rows cells(Row,Col) = ... Next 'Row Next 'Col I'm battling to get the datagrid back into a table format - and cannot Ctype(Datagrid, DataTable) directly. Any ideas how I can do this. (I'd like to be able to use the column headings and column widths of the datagrid to format the Excel sheet) Thanx Richard

    K D 2 Replies Last reply
    0
    • R RichardBerry

      Hi I have a form with 7 Datagrids. I populate DataTables from a database. I create a Dataview of the tables, and apply various filters based on user selections. I then use the DataVeiw as the Datagrid DataSource. I have also used Tablestyles to give the Datagrid Columns 'Readable' columnnames, and have adjusted the width of certain columns and set the columnwidth to 0 on some columns in order to hide them. This all works great. Now I want to add the option to Export what displayed by the datgrids to Excel. This is easy with a Datatable, since I can use the Column Names as headings, and then use a loop For Each Col as Datacoumn in dtMyTable.Columns For each Row as datarow in dtMyTable.Rows cells(Row,Col) = ... Next 'Row Next 'Col I'm battling to get the datagrid back into a table format - and cannot Ctype(Datagrid, DataTable) directly. Any ideas how I can do this. (I'd like to be able to use the column headings and column widths of the datagrid to format the Excel sheet) Thanx Richard

      K Offline
      K Offline
      KreativeKai
      wrote on last edited by
      #2

      I wish I knew an answer. I've been watching this message since you posted it, hoping someone would have the answer. I've ran into the same problem recently. If you figure out how I hope you'll followup and let me know what you find. Thanks :) Lost in the vast sea of .NET

      1 Reply Last reply
      0
      • R RichardBerry

        Hi I have a form with 7 Datagrids. I populate DataTables from a database. I create a Dataview of the tables, and apply various filters based on user selections. I then use the DataVeiw as the Datagrid DataSource. I have also used Tablestyles to give the Datagrid Columns 'Readable' columnnames, and have adjusted the width of certain columns and set the columnwidth to 0 on some columns in order to hide them. This all works great. Now I want to add the option to Export what displayed by the datgrids to Excel. This is easy with a Datatable, since I can use the Column Names as headings, and then use a loop For Each Col as Datacoumn in dtMyTable.Columns For each Row as datarow in dtMyTable.Rows cells(Row,Col) = ... Next 'Row Next 'Col I'm battling to get the datagrid back into a table format - and cannot Ctype(Datagrid, DataTable) directly. Any ideas how I can do this. (I'd like to be able to use the column headings and column widths of the datagrid to format the Excel sheet) Thanx Richard

        D Offline
        D Offline
        Dean_SF
        wrote on last edited by
        #3

        Have you tried: Dim dSource As Object dSource = DataGrid.DataSource If TypeOf dSource Is DataView Then Dim tbl As DataTable = CType(DataGrid.DataSource, DataView).Table 'Export to Excel... End If Dean

        R 1 Reply Last reply
        0
        • D Dean_SF

          Have you tried: Dim dSource As Object dSource = DataGrid.DataSource If TypeOf dSource Is DataView Then Dim tbl As DataTable = CType(DataGrid.DataSource, DataView).Table 'Export to Excel... End If Dean

          R Offline
          R Offline
          RichardBerry
          wrote on last edited by
          #4

          Hi Dean Thanks 4 your reply. That works, but the problem is that I have a table with 6000 records. This Table is that datasource for a DataView. I then apply filters to the dataview. (So for example now the Dataview only has 100 Records) It is these 100 records that I want to put in a new table for export. Using your method, it returns the full source table with all 6000 records. I'd be happy to loop through the Dataview and adding those rows to a table, but can't seem to get that right. Richard

          D 1 Reply Last reply
          0
          • R RichardBerry

            Hi Dean Thanks 4 your reply. That works, but the problem is that I have a table with 6000 records. This Table is that datasource for a DataView. I then apply filters to the dataview. (So for example now the Dataview only has 100 Records) It is these 100 records that I want to put in a new table for export. Using your method, it returns the full source table with all 6000 records. I'd be happy to loop through the Dataview and adding those rows to a table, but can't seem to get that right. Richard

            D Offline
            D Offline
            Dean_SF
            wrote on last edited by
            #5

            You can pull data directly from the dataview which would allow you to only use the filtered data: Dim i As Integer = 0 Dim dr As DataRowView For Each dr In MydataView ExcelWs.Cells(i, 1).Value = dr("Field1") ExcelWs.Cells(i, 2).Value = dr("Field2") ......... ExcelWs.Cells(i, n).Value = dr("Fieldn") etc i += 1 Next Dean

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups