Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net programmatically bind dataset to 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.

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.

like image 870
c11ada Avatar asked Jun 15 '11 13:06

c11ada


People also ask

How can create GridView programmatically in asp net?

GridView listTypeBot = new GridView(); // Create my column BoundField bf = new BoundField(); bf. HeaderText = "Types of bot available"; // Add my column to my gridview listTypeBot. Columns. Add(bf); // Create a row and a cell GridViewRow gvr = listTypeBot.


1 Answers

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.

like image 124
Kon Avatar answered Sep 26 '22 03:09

Kon