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 设置为false。
And make sure you set AutoGenerateColumns to false.
这篇关于ASP.net以编程方式将数据集绑定到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!