Click here to Skip to main content
15,886,603 members
Articles / Web Development / HTML

Formatting AutoGenerateColumns in an ASP.NET Grid

Rate me:
Please Sign up or sign in to vote.
4.78/5 (26 votes)
23 Oct 2006CPOL6 min read 187.2K   1.9K   96  
Demonstrates how to apply conditional formatting in a GridView or DataGrid when columns are dynamically generated, and wrap such code in an IExtenderProvider control.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for DataProvider
/// </summary>
public class DataProvider
{

    // Create the sample datasource for these exercises
    // this simulates a stored procedure that returns variable value columns
    public static DataTable CreateDataSource(int numValueColumns)
    {
        DataTable t = new DataTable();

        // create the table structure
        DataColumn c = new DataColumn();

        c = new DataColumn();
        c.DataType = System.Type.GetType("System.String");
        c.ColumnName = "Category";
        t.Columns.Add(c);

        for (int i = 1; i <= numValueColumns; i++)
        {
            c = new DataColumn();
            c.DataType = System.Type.GetType("System.Int32");
            c.ColumnName = "Value" + i.ToString();
            t.Columns.Add(c);
        }

        // populate the table with some sample rows of data
        Random rnd = new Random();
        DataRow r = t.NewRow();
        r["Category"] = "North Region";
        for (int i = 1; i <= numValueColumns; i++) r["Value" + i.ToString()] = rnd.Next(10, 10000);
        t.Rows.Add(r);

        r = t.NewRow();
        r["Category"] = "South Region";
        for (int i = 1; i <= numValueColumns; i++) r["Value" + i.ToString()] = rnd.Next(10, 10000);
        t.Rows.Add(r);

        r = t.NewRow();
        r["Category"] = "East Region";
        for (int i = 1; i <= numValueColumns; i++) r["Value" + i.ToString()] = rnd.Next(10, 10000);
        t.Rows.Add(r);

        r = t.NewRow();
        r["Category"] = "West Region";
        for (int i = 1; i <= numValueColumns; i++) r["Value" + i.ToString()] = rnd.Next(10, 10000);
        t.Rows.Add(r);

        return t;
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
University of Nevada, Las Vegas
United States United States
With a background in education, music, application development, institutional research, data governance, and business intelligence, I work for the University of Nevada, Las Vegas helping to derive useful information from institutional data. It's an old picture, but one of my favorites.

Comments and Discussions