Formatting AutoGenerateColumns in an ASP.NET Grid

 

GridView9foratting.aspx

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>

<!DOCTYPE html PUBLIC "-//W 3C //DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<script runat="server">

 

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            dd.SelectedValue = "1";

            BindData();

        }

    }

 

    void BindData()

    {

        DataTable dt = DataProvider.CreateDataSource(Convert.ToInt32(dd.SelectedValue));

 

        myList.DataSource = dt;

        myList.DataBind();

 

        myList2.DataSource = dt;

        myList2.DataBind();

    }

 

    void ddChange(object o, EventArgs e)

    {

        BindData();

    }

 

    private int GetCellValue(TableCell tc)

    {

        int i = -1;

 

        try { i = Convert.ToInt32(tc.Text); }

        catch { /* do nothing; */}

 

        return i;

    }

 

    private void GV_RowDataBound(object o, GridViewRowEventArgs e)

    {

        // apply custom formatting to data cells

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            // set formatting for the category cell

            TableCell cell = e.Row.Cells[0];

            cell.Width = new Unit("120px");

            cell.Style["border-right"] = "2px solid #666666";

            //cell.BackColor = System.Drawing.Color.LightGray;

            cell.BackColor = System.Drawing.Color.Blue;

 

            // set formatting for value cells

            for (int i = 1; i < e.Row.Cells.Count; i++)

            {

                cell = e.Row.Cells[i];

 

                // right-align each of the column cells after the first

                // and set the width

                cell.HorizontalAlign = HorizontalAlign.Right;

                cell.Width = new Unit("90px");

 

                // alternate background colors

                if (i % 2 == 1)

                    cell.BackColor

                      = System.Drawing.ColorTranslator.FromHtml("#EFEFEF");

 

                // check value columns for a high enough value

                // (value >= 8000) and apply special highlighting

                if (GetCellValue(cell) >= 8000)

                {

                    cell.Font.Bold = true;

                    cell.BorderWidth = new Unit("1px");

                    cell.BorderColor = System.Drawing.Color.Gray;

                    cell.BorderStyle = BorderStyle.Dotted;

                    cell.BackColor = System.Drawing.Color.Honeydew;

                }

 

            }

        }

 

        // apply custom formatting to the header cells

        if (e.Row.RowType == DataControlRowType.Header)

        {

            foreach (TableCell cell in e.Row.Cells)

            {

                cell.Style["border-bottom"] = "2px solid #666666";

                //cell.BackColor = System.Drawing.Color.LightGray;

                cell.BackColor = System.Drawing.Color.DarkOrange;

 

            }

        }

 

    }

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Formatting Columns in a AutoGenerateColumns GridView</title>

    <style type="text/css">

            body {font-family: 'Tahoma'; font-size: 10pt ;}

            td {font-size: 10pt ;}

            code {font-family: 'monospace'; font-size: 10pt ; color: maroon;}

            code.blue {color: blue;}

            a {text-decoration: none; color: blue;}

    </style>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <h3>

            Formatting Columns in a AutoGenerateColumns GridView</h3>

 

        <p>

            This is a simulation of a <code>GridView</code> accepting data from

            a source that provides dynamic columns.  This could, for example,

            be a stored procedure that presents sales-to-date totals by a

            category, where the columns could be quarters- or months-to-date.

            The values presented in this example are randomly generated.

            <br /><br />

            <code>AutoGenerateColumns</code> is set to <code class="blue">true</code>, and

            a custom function is supplied for the <code>RowDataBound</code> event            

            to apply formatting.  Among other things, the function ensures

            that each dynamically added value column is right-aligned, and

            high sales numbers (numbers 8000 and above) are displayed with special

            hilighting.

        </p>       

        <hr />

        Number of dynamic value columns to present:

        <asp:DropDownList id="dd" runat="server" AutoPostBack="true"

                          OnSelectedIndexChanged="ddChange">

            <asp:ListItem text="1" value="1" />

            <asp:ListItem text="2" value="2" />

            <asp:ListItem text="3" value="3" />

            <asp:ListItem text="4" value="4" />

            <asp:ListItem text="5" value="5" />

        </asp:DropDownList>

       

        <br /><br />

       

        <asp:GridView id="myList" runat="server"

                      AutoGenerateColumns="true"

                      Gridlines="none"

                      CellPadding="4"

                      BorderStyle="Solid"

                      BorderWidth="1"

                      BorderColor="Black"

                      OnRowDataBound="GV_RowDataBound"

                      >

        </asp:GridView>                     

       

        <br /><br />

        <p>For comparison, the GridView below is identical to the first,

           except that its RowDataBound event is not trapped.</p>

        <asp:GridView id="myList2" runat="server"

                      AutoGenerateColumns="true"

                      Gridlines="none"

                      CellPadding="4"

                      BorderStyle="Solid"

                      BorderWidth="1"

                      BorderColor="Black"

                      >

        </asp:GridView>                     

    </div>

    </form>

</body>

</html>

 

DataProvider.cs

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>

/// DataProvider 的摘要说明

/// </summary>

public class DataProvider

{

     public DataProvider()

     {

         //

         // TODO: 在此处添加构造函数逻辑

         //

     }

 

    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;

    }

}

 

More information:  http://www.codeproject.com/aspnet/FrmtAutoGenClmnASPNETGrid.asp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值