在 gridview 的标题中创建一个下拉列表,同时从 Excel 工作表填充其列数据
我需要创建一个 gridview,它
- 用 Excel 工作表中的数据填充其列,
- 对于 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
- fills its column with data from an excel sheet and
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您的数据集或数据表不为空。如果没有行,则不会出现任何内容。这是我做的一个小测试,Ddl 出现在标题中。只需在新页面上运行该示例即可。
HTML
VB
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
VB
有几件事。
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.