且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET编程将数据集绑定到gridview

更新时间:2022-10-15 07:46:54

所以你打算在运行时创建列?尝试这样:



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.