0

在aspx页面的代码后面,我有一个数据表:

            Dim people As DataTable = New DataTable()

            people.Columns.Add("ID", System.Type.GetType("System.Int32"))
            people.Columns.Add("FirstName", System.Type.GetType("System.String"))
            people.Columns.Add("LastName", System.Type.GetType("System.String"))

            people.Rows.Add(10, "Merci", "Beaucoup")

这是GridViewData:(不是asp:GridView)

        Dim gvPeople As System.Windows.Forms.DataGridView = New DataGridView()


        gvPeople.AutoGenerateColumns = False

        'Set Columns Count
        gvPeople.ColumnCount = 3

        'Add Columns
        gvPeople.Columns(0).Name = "ID"
        gvPeople.Columns(0).HeaderText = "ID"
        gvPeople.Columns(0).DataPropertyName = "ID"

        gvPeople.Columns(1).Name = "FirstName"
        gvPeople.Columns(1).HeaderText = "FirstName"
        gvPeople.Columns(1).DataPropertyName = "FirstName"

        gvPeople.Columns(2).Name = "LastName"
        gvPeople.Columns(2).HeaderText = "LastName"
        gvPeople.Columns(2).DataPropertyName = "LastName"

这里我将DataGridView的数据源设置为DataTable:

        gvPeople.DataSource = people

当我使用 DataGridView 将 DataGridView 导入 Excel 工作表Gembox.Spreadsheet时,它只显示 DataGridView 的 headerText 而没有数据。这是导入代码:

        DataGridViewConverter.ImportFromDataGridView(ws, gvPeople, New ImportFromDataGridViewOptions() With _
            {
               .ColumnHeaders = True,
               .StartRow = 8,
               .StartColumn = 0
             })

我尝试了多种方法,例如:

  • 设置.DataMemberDataTable 名称:gvPeople.DataMember=people.TableName

  • Refresh()Update()分配.Datasource.

注意:这不是 a asp:GridView,它是 aDataGridView并且没有DataBind()方法。

4

3 回答 3

3

如果使用asp:GridView,只需使用该databind功能

gvPeople.DataSource = people
gvPeople.Databind()

注意:不建议在 Web 项目中使用 datagridView,该控件是为 winforms 应用程序设计的。

如果使用windows.Forms.DataGridView

DataGridView必须添加到表单控件集合以执行其布局

Me.Controls.Add(gvPeople)

但这在使用 Web 项目时无法完成(出现错误)

所以你必须做一些解决方法

首先,我创建了一个将数据行转换为字符串数组的函数

Public Function ToStringArray(ByVal dRow As DataRow) As String()

    Dim lst As New List(Of Object)
    lst.AddRange(dRow.ItemArray)

    Return lst.Select(Function(x) x.ToString).ToArray()

End Function

然后我使用了以下代码

这是你的代码:

    Dim people As DataTable = New DataTable("people")

    people.Columns.Add("ID", System.Type.GetType("System.Int32"))
    people.Columns.Add("FirstName", System.Type.GetType("System.String"))
    people.Columns.Add("LastName", System.Type.GetType("System.String"))

    people.Rows.Add(10, "Merci", "Beaucoup")

    Dim gvPeople As System.Windows.Forms.DataGridView = New System.Windows.Forms.DataGridView()


    gvPeople.AutoGenerateColumns = False

    ''Set Columns Count
    gvPeople.ColumnCount = 3

    ''Add Columns
    gvPeople.Columns(0).Name = "ID"
    gvPeople.Columns(0).HeaderText = "ID"
    gvPeople.Columns(0).DataPropertyName = "ID"

    gvPeople.Columns(1).Name = "FirstName"
    gvPeople.Columns(1).HeaderText = "FirstName"
    gvPeople.Columns(1).DataPropertyName = "FirstName"

    gvPeople.Columns(2).Name = "LastName"
    gvPeople.Columns(2).HeaderText = "LastName"
    gvPeople.Columns(2).DataPropertyName = "LastName"

这是我添加的代码

    For Each drow As DataRow In people.Rows
        gvPeople.Rows.Add(ToStringArray(drow))
    Next

    gvPeople.RowCount = people.Rows.Count

    gvPeople.Refresh()
于 2017-02-01T17:13:02.160 回答
2

您真的应该重新考虑这种方法(在 ASP.NET 应用程序中使用 DataGridView)。

如果只想将 GridView 控件中的数据插入 Excel 文件,请使用 DataTable。

正如您在评论中提到的,您想要自定义要导出的列和列的顺序。在这种情况下,您只需要调整 DataTable 本身,例如尝试以下操作:

' Sample DataTable.
Dim people As New DataTable()

people.Columns.Add("ID", GetType(Integer))
people.Columns.Add("FirstName", GetType(String))
people.Columns.Add("LastName", GetType(String))

people.Rows.Add(1, "John", "Doe")
people.Rows.Add(2, "Jane", "Doe")
people.Rows.Add(10, "Merci", "Beaucoup")

' Remove "ID" column.
people.Columns.Remove("ID")

' Set "LastName" column on first place.
people.Columns("LastName").SetOrdinal(0)

' Create Excel file.
Dim ef As New ExcelFile()
Dim ws As ExcelWorksheet = ef.Worksheets.Add("Sheet1")

' Insert DataTable into Excel file.
Dim op As New InsertDataTableOptions()
op.ColumnHeaders = True
ws.InsertDataTable(people, op)

' Save Excel file.
ef.Save("Sample.xlsx")

我希望这有帮助。
另外,如果您对此感兴趣,您将如何使用 GemBox.Spreadsheet 将数据以及 GridView 控件中的样式和格式插入到 Excel 文件中: https ://www.gemboxsoftware.com/support-center/kb /articles/export-gridview-and-or-datagrid-control-to-excel-file

于 2017-02-02T13:17:35.687 回答
0

除了在 asp.net 应用程序中使用 datagrid 之外,您还可以使用 asp gridview,这可以根据下面的链接

 https://www.gemboxsoftware.com/spreadsheet/examples/asp-net-excel-export/5101
于 2017-02-01T17:42:41.257 回答