ASP.NET编程将数据集绑定到gridview [英] ASP.net programatically bind dataset to gridview
问题描述
时刻只是在做
GridView1.DataSource = ds;
GridView1.DataBind();
但这显然将数据集中的所有列绑定到gridview。
所以你打算在运行时创建列?尝试这样:
http:/ /www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx
或者,您可以在aspx中提前设置gridview:
<列>
< asp:BoundField DataField =ProductNameHeaderText =ProductSortExpression =ProductName//>
< asp:BoundField DataField =CategoryNameHeaderText =CategoryReadOnly =TrueSortExpression =CategoryName/>
< asp:BoundField DataField =UnitPriceDataFormatString ={0:c}HeaderText =PriceHtmlEncode =FalseSortExpression =UnitPrice/>
< / Columns>
并确保设置 AutoGenerateColumns 为false。
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.
at the moment im just doing
GridView1.DataSource = ds;
GridView1.DataBind();
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
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>
And make sure you set AutoGenerateColumns to false.
这篇关于ASP.NET编程将数据集绑定到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!