Home Articles My Account Messages Tools Join
Tech Blog, ASP.Net, VB.Net, C#.Net, Programming Help, Help Guide
StellarPC.com | Dynamically Building Gridview Rows and Columns (ASP.Net)
Dynamically Building Gridview Rows and Columns (ASP.Net)
Written by: Evan Cummings

Dynamically Building Gridview Rows and Columns

The Gridview control, successor to the Datagrid introduced in ASP.NET 2.0, is an exceptionally flexible control for displaying tabular data. We can bind nearly any source of data to it and it will do its magic and display our results for us. It is rarely as simple as that, however, as data is typically manipulated, formatted, and processed into new and more useful forms before we display that data to the user. At times it may be necessary to build our gridview dynamically, based on the data provided to this. Luckily this is a straightfoward process to implement. Lets examine how we can conditionally restrict rows from being included in our GridView by completely building our table in our code behind.

The GridView (.aspx)

The GridView itself can be very simple - style and formatting are all up to your tastes and your sites aesthetics. What we need to be really cetain of is that a certain property gets included with our GridView in its asp tag:



<asp:GridView id="GridView1" runat="server" AutoGenerateColumns="False">
...
</asp:GridView>



It is essential that we include the bolded property, AutoGenerateColumns, and set it to false. What this does is it prevents the GridView from automatically generating its columns and headers based on the dataset datasource we provide it. Since we are building this as a dynamic piece, we do not want the GridView to help us out with this at all.

The Code-Behind (.vb)

All of our heavy lifting will be in the code behind of the page itself. I am assuming we have previously retrieved a dataset of prices, priceSet, that contains a minimum purchase field, and price field, and a potential third column with any additional pricing added in. The catch is that, if there is no additional pricing, I do not want to show a column filled simply with 0's - that is not especially good looking from a user interface perspective. I would rather first determine whether or not there are any additional pricing, and if there is, I would then want to display an additional column with that pricing in it. Additionally, I only want to show this price table is there is more than price for the product.

To acheive these objectives, lets first build a couple BoundFields, which exist to contain the data we are supplying it.



VB.NET
Dim priceSet As DataSet = ds 'Previously Retrieved Dataset

'Build Bound Columns
Dim minPurchase As New BoundField
minPurchase.HeaderText = "Minimum Purchase"
minPurchase.DataField = "MINIMUM"
GridView1.Columns.Add(minPurchase)

Dim pricePer As New BoundField
pricePer.HeaderText = "Price Per Item"
pricePer.DataField = "PRICE_PER"
pricePer.DataFormatString = "${0}"
GridView1.Columns.Add(pricePer)

C#
DataSet priceSet = ds;

BoundField minPurchase = New BoundField();
minPurchase.HeaderText = "Minimum Purchase";
minPurchase.DataField = "MINIMUM";
GridView1.Columns.Add(minPurchase);

BoundField pricePer = New BoundField();
pricePer.HeaderText = "Price Per Item";
pricePer.DataField = "PRICE_PER";
pricePer.DataFormatString = "${0}";
GridView1.Columns.Add(pricePer);



At this point, we want to ensure that we have more than one price (as we want to hide all of this information if there is not more than one). We can do this by going to our priceset and count the rows to ensure there are more than one. If this is true, we need to turn our focus toward checking to see if it is necessary to show our third additional price column. To do this, we should enumerate through our priceset collection and keep a tab on this value to see if it increase.



VB.NET

If priceSet.Tables(0).Rows.Count > 1 Then

Dim count As Double = 0
Dim i As Integer

For i = 0 To priceSet.Tables(0).Rows.Count - 1
count += Convert.ToDouble(priceSet.Tables(0).Rows(i)("SURCHARGE"))
Next

C#

if (priceSet.Tables[0].Rows.Count - 1) {
double count = 0;

for (int i = 0; i < priceSet.Tables[0].Rows.Count - 1; i++) {
count += Convert.ToDouble(priceSet.Tables[0].Rows[i]("SURCHARGE"))
}



At this point, we have determined that if there is more than one row in our dataset, we will loop through the SURCHARGE column, counting the values. This is where we can now dynamically add this column to our gridview, ensuring that it only display when our conditions are met:



VB.NET

If count > 0 Then

Dim basePrice As New BoundField
basePrice.HeaderText = "Base Price"
basePrice.DataField = "SURCHARGE"
basePrice.DataFormatString = "{0:c}"
gvPricePoints.Columns.Add(basePrice)

End If

C#

if (count > 0) {
BoundField basePrice = New BoundField();
basePrice.HeaderText = "Base Price";
basePrice.DataField = "SURCHARGE";
basePrice.DataFormatString = "{0:c}";
GridView1.Columns.Add(basePrice);
}



We simply determine if our counter has been incremented to our specifications, and if it has, we build a new BoundField on the fly and add it to the Columns collection of our GridView. Using this method, we have strictly limited what may be part of our GridView through our own custom logic. To finish off our example, we need to either show the GridView and bind its data (if our conditions were met), or disable it if our conditions were not met.



VB.NET

GridView1.Visible = True
GridView1.DataSource = priceSet
GridView1.DataBind()

Else
GridView1.Visible = False
End If

C#

GridView1.Visible = True;
GridView1.DataSource = priceSet;
GridView1.DataBind();

}

else {

GridView1.Visible = False;

}



Building our GridView in our code behinds gives us a much greater degree of freedom in what we are ultimately displaying to the user. As you saw throughout this code, we can set any property of the GridView from code, allowing us enormous control over the logic that goes into its creation.

Content - ASP.NET, GridView, C#, VB.NET
New Post Next 50 | Previous 50
Post# Subject: Posted By: Time:
New Post Next 50| Previous 50