在 gridview 的标题中创建一个下拉列表,同时从 Excel 工作表填充其列数据

发布于 2024-12-17 01:06:41 字数 1940 浏览 1 评论 0原文

我需要创建一个 gridview,它

  1. 用 Excel 工作表中的数据填充其列,
  2. 对于 gridview 中的每一列,标题应包含一个下拉列表,其中包含电话、名称、价格等列表项,用户将选择这些列表项并将其设置为标题对于特定的列。

我将数据导入到 gridview 中,但无法在 gridview 中创建下拉列表。 如果我尝试在设计器页面中创建下拉列表,它只是在 gridview 标题中创建下拉列表,但不填充数据。那么如何在 gridview 标题中创建下拉列表,同时从 Excel 工作表填充其列数据。请帮忙。

我用来将数据填充到 gridview 中的代码是

Dim con As String = ""
    Select Case Extension
        Case ".xls"
            'Excel 97-03
            con=ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString()
            Exit Select
        Case ".xlsx"
            'Excel 07
            con =ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString()
            Exit Select
    End Select
    con = String.Format(con,Path)
    Dim connExcel As New OleDbConnection(con)
    Dim str As String = "SELECT * From [Sheets$]"
    Dim cmdExcel As New OleDbCommand(str, connExcel)
    Dim da As New OleDbDataAdapter(cmdExcel)
    Dim dset As New DataSet()
    da.Fill(dset, "Tabledata")
    Dim dtable As DataTable = ds.Tables(0)
    GridView1.DataSource = dset.Tables(0).DefaultView
    GridView1.DataBind()

,创建下拉列表的代码是

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>             
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"   AutoPostBack="True">
                            <asp:ListItem Text="name " Value="Description of Goods" />
                            <asp:ListItem Text="telephone" Value="Count" />
                            <asp:ListItem Text="price" Value="Weight" />
                            <asp:ListItem Text="Value" Value="Value" />
                        </asp:DropDownList>
                    </HeaderTemplate>

I need to create a gridview which

  1. fills its column with data from an excel sheet and
  2. for each column in the gridview ,the header should contain a drop down list with listitems like telephone,name,price which the user will select and set as the header for the particular column.

I imported data into gridview but i am not able to create a drop down list in the gridview.
If i try to create a dropdownlist in the designer page,it is just creating the dropdownlist in the gridview header but not populating the data.So How can i create a dropdownlist in the header of the gridview while populating its column data from excel sheet. Please help.

the code i used to populate data into gridview is

Dim con As String = ""
    Select Case Extension
        Case ".xls"
            'Excel 97-03
            con=ConfigurationManager.ConnectionStrings("Excel03ConString").ConnectionString()
            Exit Select
        Case ".xlsx"
            'Excel 07
            con =ConfigurationManager.ConnectionStrings("Excel07ConString").ConnectionString()
            Exit Select
    End Select
    con = String.Format(con,Path)
    Dim connExcel As New OleDbConnection(con)
    Dim str As String = "SELECT * From [Sheets$]"
    Dim cmdExcel As New OleDbCommand(str, connExcel)
    Dim da As New OleDbDataAdapter(cmdExcel)
    Dim dset As New DataSet()
    da.Fill(dset, "Tabledata")
    Dim dtable As DataTable = ds.Tables(0)
    GridView1.DataSource = dset.Tables(0).DefaultView
    GridView1.DataBind()

and the code to create drop down list is

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
            <Columns>             
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server"   AutoPostBack="True">
                            <asp:ListItem Text="name " Value="Description of Goods" />
                            <asp:ListItem Text="telephone" Value="Count" />
                            <asp:ListItem Text="price" Value="Weight" />
                            <asp:ListItem Text="Value" Value="Value" />
                        </asp:DropDownList>
                    </HeaderTemplate>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飞烟轻若梦 2024-12-24 01:06:41

确保您的数据集或数据表不为空。如果没有行,则不会出现任何内容。这是我做的一个小测试,Ddl 出现在标题中。只需在新页面上运行该示例即可。

HTML

<asp:GridView ID="GridView1" runat="server">
      <Columns>
      <asp:TemplateField>
      <HeaderTemplate>

        <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="name " Value="Description of Goods" />
                            <asp:ListItem Text="telephone" Value="Count" />
                            <asp:ListItem Text="price" Value="Weight" />
                            <asp:ListItem Text="Value" Value="Value" />
        </asp:DropDownList>
      </HeaderTemplate>
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("TestItem") %>'></asp:Label>
      </ItemTemplate>
      </asp:TemplateField>
      </Columns>
      </asp:GridView>

VB

Public Class DllInGvHeader
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
      Dim tList As New List(Of Test)
      Dim t As New Test
      t.TestItem = 1
      tList.Add(t)
      t.TestItem = 2
      tList.Add(t)
      GridView1.DataSource = tList
      GridView1.DataBind()
    End If
  End Sub

End Class

Public Class Test
  Public Property TestItem As String
End Class

Make sure that your dataset or datatable is not empty. If there are no rows, nothing will appear. Here's a little test I did and the Ddl appeared in the header. Just run the example as is on a new page.

HTML

<asp:GridView ID="GridView1" runat="server">
      <Columns>
      <asp:TemplateField>
      <HeaderTemplate>

        <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem Text="name " Value="Description of Goods" />
                            <asp:ListItem Text="telephone" Value="Count" />
                            <asp:ListItem Text="price" Value="Weight" />
                            <asp:ListItem Text="Value" Value="Value" />
        </asp:DropDownList>
      </HeaderTemplate>
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Eval("TestItem") %>'></asp:Label>
      </ItemTemplate>
      </asp:TemplateField>
      </Columns>
      </asp:GridView>

VB

Public Class DllInGvHeader
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
      Dim tList As New List(Of Test)
      Dim t As New Test
      t.TestItem = 1
      tList.Add(t)
      t.TestItem = 2
      tList.Add(t)
      GridView1.DataSource = tList
      GridView1.DataBind()
    End If
  End Sub

End Class

Public Class Test
  Public Property TestItem As String
End Class
哎呦我呸! 2024-12-24 01:06:41

有几件事。
1. 确保 Gridview 中的 AutoGenerateColumns 设置为“True”。
2. 在您的代码中,
“将 dtable 调暗为 DataTable = ds.Tables(0)”
应该改为
“Dim dtable As DataTable = dset.Tables(0)”,这是您正在填充的数据集。

另外,如果 AutoGenerateColumns 设置为 True,Gridview 将自动显示所有列,因此如果您的数据集不为空,则会显示一些内容。另外,您不必使用defaultView,只需使用 dset.Tables(0) 作为数据源即可。一旦您可以查看 gridview,请记下所有列标题名称,这些名称可以使用 Eval 进行绑定,就像我之前的示例一样。

A couple of things.
1. Make sure AutoGenerateColumns is set to "True" in the Gridview.
2. In your code,
"Dim dtable As DataTable = ds.Tables(0)"
should be changed to
"Dim dtable As DataTable = dset.Tables(0)", which is the dataset you are filling.

Also, Gridview will show all the columns automatically if AutoGenerateColumns is set to True, so if your dataset is not empty, something will show up. Also you don't have to use defaultView, just the dset.Tables(0) would do as the datasource. Once you can view the gridview, note all the column header names, which can be bound using Eval like in my example before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文