ASP.net 以编程方式将数据集绑定到 gridview [英] ASP.net programmatically bind dataset to gridview
问题描述
我有一个大约有 15 列的数据集,我还有一个 ASP.net gridview.我想知道是否有人知道如何使用数据集填充 gridview,但问题是我只想要数据集中的几列.
I have a data set with about 15 columns, and I also have an ASP.net gridview. I was wondering if any one knew how I can populate the gridview with the dataset, but the issue is I just want a few of the columns from the dataset.
目前我只是在做
GridView1.DataSource = ds;
GridView1.DataBind();
但这显然将数据集中的所有列绑定到 gridview.
but this obviously binds all the columns from the dataset to the gridview.
推荐答案
所以您希望在运行时创建列?试试这个:
So you are looking to create columns at runtime? Try this:
http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx
或者,您可以在 aspx 中提前配置您的 gridview:
Alternatively, you can configure your gridview ahead of time in the aspx:
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product" SortExpression="ProductName" />
<asp:BoundField DataField="CategoryName" HeaderText="Category" ReadOnly="True" SortExpression="CategoryName" />
<asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="Price" HtmlEncode="False" SortExpression="UnitPrice" />
</Columns>
并确保您设置了 AutoGenerateColumns 为假.
And make sure you set AutoGenerateColumns to false.
这篇关于ASP.net 以编程方式将数据集绑定到 gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!