ASP.NET编程将数据集绑定到gridview [英] ASP.net programatically bind dataset to gridview

查看:217
本文介绍了ASP.NET编程将数据集绑定到gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集约15列,我也有一个ASP.net网格视图。我想知道如果有人知道如何使用数据集填充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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆