我试图在“Flipdatagride”按钮的工作方式中合并以下Codesample,将数据从水平旋转到垂直,这是c#
此时我在vb.net 2013中创建的My Application中的数据是数据网格视图水平呈现,顶部有一个列标题,输入文本位于每个单独标题下的文本框或点击框中基于行。
这是从c#到vb.net的代码示例转换
Public Class Form1
Private ops As New Operations
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()
dt = GetCustomers()
ds.Tables.Add(dt)
Dim my_DataView As DataView = ds.Tables(0).DefaultView
DataGridView1.DataSource = my_DataView
End Sub
Private Function GetCustomers() As DataTable
Dim table As New DataTable()
table.TableName = "Customers"
table.Columns.Add("Name", GetType(String))
table.Columns.Add("Price", GetType(String))
table.Columns.Add("Country", GetType(String))
table.Rows.Add(New Object() {"Mohamad", "1700", "Egypt"})
table.Rows.Add(New Object() {"Tarek", "550", "Syria"})
table.Rows.Add(New Object() {"Gamal", "762", "Saudi Arabia"})
table.AcceptChanges()
Return table
End Function
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 butFlip_Click(sender As Object, e As EventArgs) Handles butFlip.Click
Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
DataGridView1.DataSource = currentDataView
butFlip.Enabled = False
butNormal.Enabled = True
End Sub
Private Sub butNormal_Click(sender As Object, e As EventArgs) Handles butNormal.Click
Dim currentDataSet As DataView = ds.Tables(0).DefaultView
DataGridView1.DataSource = currentDataSet
butFlip.Enabled = True
butNormal.Enabled = False
End Sub
Private Sub butExit_Click(sender As Object, e As EventArgs) Handles butExit.Click
Close()
End Sub
Private Sub butExport_Click(sender As Object, e As EventArgs) Handles butExport.Click
If Not String.IsNullOrWhiteSpace(txtSheetName.Text) Then
Select Case ops.ExportDataTable(CType(DataGridView1.DataSource, DataView).Table, txtSheetName.Text)
Case OperationResults.Success
MessageBox.Show("Data written to file")
Case OperationResults.SheetExist
MessageBox.Show("Sheet already exists, aborted")
Case OperationResults.UnknownException
MessageBox.Show($"Operation throw an exception{Environment.NewLine}{ops.Exception.Message}")
End Select
End If
End Sub
End Class`
我想要实现的是,当用户在我的应用程序中选择“生成”按钮时,它将首先将数据网格翻转为垂直,并使用以上代码示例中的以下代码以相同格式导出为excel;
Private Sub butFlip_Click(sender As Object, e As EventArgs) Handles
butFlip.Click
Dim currentDataSet As DataSet = FlipDataSet(ds) ' Flip the DataSet
Dim currentDataView As DataView = currentDataSet.Tables(0).DefaultView
DataGridView1.DataSource = currentDataView
butFlip.Enabled = False
butNormal.Enabled = True
End Sub
Private Sub butNormal_Click(sender As Object, e As EventArgs) Handles
butNormal.Click
Dim currentDataSet As DataView = ds.Tables(0).DefaultView
DataGridView1.DataSource = currentDataSet
butFlip.Enabled = True
butNormal.Enabled = False
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
到目前为止我已经得到了这个但是button2'Flipdata'只是将datagridview留空了一个名为'0'的列标题。有人有什么建议吗?
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`