Datagridview水平到垂直按钮

时间:2017-06-04 18:05:43

标签: vb.net datagridview

以下代码在导出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

1 个答案:

答案 0 :(得分:0)

已经回答了:

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