Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 I'm just doing

    GridView1.DataSource = ds;
    GridView1.DataBind();

but this obviously binds all the columns from the dataset to the gridview.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
328 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...