【问题标题】:Datagridview Horizontal to vertical buttonDatagridview 水平到垂直按钮
【发布时间】:2017-06-04 18:05:43
【问题描述】:

以下代码在导出 datagridview 数据时无法正常工作,当试图使其垂直时,左侧的标题以及每个旁边的文本。翻转后,用户将单击 button1 以导出到 excel。

Imports System.Data.DataTable
Imports System.IO
Imports Microsoft.Office.Interop
Public Class Form1
Dim table As New DataTable(0)
Public checkBoxList As List(Of CheckBox)
Private ds As DataSet = Nothing
Private dt As DataTable = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ds = New DataSet()
    dt = New DataTable()
    ds.Tables.Add("Table")
    Dim my_DataView As DataView = ds.Tables(0).DefaultView
    DataGridView1.DataSource = my_DataView
    table.Columns.Add("Forename", Type.GetType("System.String"))
    table.Columns.Add("Surname", Type.GetType("System.String"))
    table.Columns.Add("Food", Type.GetType("System.String"))
    checkBoxList = New List(Of CheckBox) From {CheckBox1, CheckBox2, CheckBox3, CheckBox4}


End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim values As String = "" &
    String.Join(" & ", checkBoxList _
    .Where(Function(cb) cb.Checked).Select(Function(cb) cb.Text))

    ' use values for placing into your DataGridView
    CheckBox1.Text = values
    CheckBox2.Text = values
    CheckBox3.Text = values
    CheckBox4.Text = values


    table.Rows.Add(TextBox1.Text, TextBox2.Text, values.ToString)
    DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    DataGridView1.RowTemplate.Height = 100
    DataGridView1.AllowUserToAddRows = False
    DataGridView1.DataSource = table

    'Save to excel with headers
    Dim ExcelApp As Object, ExcelBook As Object
    Dim ExcelSheet As Object
    Dim i As Integer
    Dim j As Integer

    'create object of excel
    ExcelApp = CreateObject("Excel.Application")
    ExcelBook = ExcelApp.WorkBooks.Add
    ExcelSheet = ExcelBook.WorkSheets(1)

    With ExcelSheet
        For Each column As DataGridViewColumn In DataGridView1.Columns
            .cells(1, column.Index + 1) = column.HeaderText
        Next
        For i = 1 To Me.DataGridView1.RowCount
            .cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("Forename").Value
            For j = 1 To DataGridView1.Columns.Count - 1
                .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value
            Next
        Next

    End With

    ExcelApp.Visible = True
    '
    ExcelSheet = Nothing
    ExcelBook = Nothing
    ExcelApp = Nothing
End Sub

Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

End Sub
Public Function FlipDataSet(ByVal my_DataSet As DataSet) As DataSet
    Dim ds As New DataSet()

    For Each dt As DataTable In my_DataSet.Tables
        Dim table As New DataTable()

        For i As Integer = 0 To dt.Rows.Count
            table.Columns.Add(Convert.ToString(i))
        Next
        Dim r As DataRow
        For k As Integer = 0 To dt.Columns.Count - 1
            r = table.NewRow()
            r(0) = dt.Columns(k).ToString()
            For j As Integer = 1 To dt.Rows.Count
                r(j) = dt.Rows(j - 1)(k)
            Next
            table.Rows.Add(r)
        Next

        ds.Tables.Add(table)
    Next

    Return ds
End Function

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
    DataGridView1.DataSource = currentDataView

    Button2.Enabled = False

End Sub
End Class

当点击 button2 时,它应该使用以下内容翻转数据;

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles 
    Button2.Click
    Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
    Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
    DataGridView1.DataSource = currentDataView

    Button2.Enabled = False

    End Sub
    End Class

我尝试过调试,但我似乎找不到任何问题?它允许我在选择复选框的同时在文本框中插入数据,并在单击按钮 1 以导出它时工作正常,但它不会翻转数据。

任何人都可以建议如何解决这个问题,因为我在 6 月 8 日有一个演示文稿,这些数据需要自动翻转

源码:Download Myproject

Image of target

【问题讨论】:

    标签: vb.net datagridview


    【解决方案1】:

    回答:

    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    Private ds As DataSet = Nothing
    Private dt As DataTable = Nothing
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.AllowUserToAddRows = False
    
        dt = New DataTable("MyTable")
        dt.Columns.Add("Forename", Type.GetType("System.String"))
        dt.Columns.Add("Surname", Type.GetType("System.String"))
        dt.Columns.Add("Food", Type.GetType("System.String"))
    
        ds = New DataSet
        ds.Tables.Add(dt)
    
        Dim my_DataView As DataView = ds.Tables("MyTable").DefaultView
        DataGridView1.DataSource = my_DataView
    End Sub
    
    Private Sub Button_AddRowData_Click(sender As Object, e As EventArgs) Handles Button_AddRowData.Click
        Dim foods As String = String.Join(" & ", CheckedListBox1.CheckedItems.Cast(Of String))
        dt.Rows.Add(New Object() {TextBox_Forename.Text, TextBox_Surname.Text, foods})
    End Sub
    
    Private Sub Button_FlipAndSave_Click(sender As Object, e As EventArgs) Handles Button_FlipAndSave.Click
        FlipAndSave(ds.Tables("MyTable"))
    End Sub
    
    Private Sub FlipAndSave(table As DataTable)
        Dim ExcelApp As New Excel.Application
        Dim WrkBk As Excel.Workbook = ExcelApp.Workbooks.Add()
        Dim WrkSht As Excel.Worksheet = CType(WrkBk.Worksheets(1), Excel.Worksheet)
    
        With WrkSht
            For ci As Integer = 0 To table.Columns.Count - 1
                .Cells(ci + 1, 1) = table.Columns(ci).ColumnName
            Next
            For ri As Integer = 0 To table.Rows.Count - 1
                For ci As Integer = 0 To table.Columns.Count - 1
                    .Cells(ci + 1, ri + 2) = table.Rows(ri).Item(ci).ToString
                Next
            Next
        End With
    
        ExcelApp.Visible = True
    
        'use this lines if you want to automatically save the WorkBook
        'WrkBk.SaveAs("C:\Some Folder\My Workbook.xlsx") '(.xls) if you have an old version of Excel
    
        'ExcelApp.Quit() 'use this line if you want to close the Excel Application
    
        ReleaseObject(ExcelApp)
        ReleaseObject(WrkBk)
        ReleaseObject(WrkSht)
    End Sub
    
    Private Sub ReleaseObject(obj As Object)
        Marshal.ReleaseComObject(obj)
        obj = Nothing
    End Sub
    End Class
    

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 1970-01-01
      • 2010-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多