- 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;
- using System.Drawing;
- /// <summary>
- /// 建立该类,来调整数据控件的行数,及行数宽度。并且构造颜色的方法,对数据行进行填充,
- /// 数据控件有DataGrid和GridView,针对这两个
- /// 控件各自建立不同的方法
- /// </summary>
- public class DataRowColor
- {
- //私有属性
- private int _num;
- private int _total;
- private int _cellcount;
- private string _OddColor = "#FFFFFF";
- private string _EvenColor = "#B9FEB7";
- private string _mousecolor = "#63F087";
- public DataRowColor()
- {
- //
- // TODO: 在此处添加构造函数逻辑
- //
- }
- //构造函数,初始化颜色值
- public DataRowColor(string OddColor, string EvenColor, string MouseColor)
- {
- this._OddColor = OddColor;
- this._EvenColor = EvenColor;
- this._mousecolor = MouseColor;
- }
- //公共属性
- public int num
- {
- set { this._num = value; }
- get { return this._num; }
- }
- public int total
- {
- set { this._total = value; }
- get { return this._total; }
- }
- public int cellcount
- {
- set { this._cellcount = value; }
- get { return this._cellcount; }
- }
- public string OddColor
- {
- set { this._OddColor = value; }
- get { return this._OddColor; }
- }
- public string EvenColor
- {
- set
- {
- this._EvenColor = value;
- }
- get
- {
- return this._EvenColor;
- }
- }
- public string mousecolor
- {
- set { this._mousecolor = value; }
- get { return this._mousecolor; }
- }
- //将颜色值分解,转化为RGB三色值的方法
- private int convert(string color, int index, int length)
- {
- string i = color.Substring(index, length);
- int j = Convert.ToInt32(i, 16);
- return j;
- }
- /*================================定义DataGrid的背景色,和鼠标经过时的颜色============================*/
- public void SetDataGrid(DataGrid dg, DataGridItemEventArgs e, int height)
- {
- //奇数行颜色
- // OddColor = "#FFFFFF";
- //偶数行颜色
- //EvenColor = "#FFFBD6";
- e.Item.Height = Unit.Pixel(height);
- //调用颜色转化函数
- int r = convert(EvenColor, 1, 2);
- int g = convert(EvenColor, 3, 2);
- int b = convert(EvenColor, 5, 2);
- if (num % 2 == 0)
- {
- //注:此处的颜色值只能用rgb三色值,不能用16位的值,不知为何,16位无法显示
- e.Item.BackColor = System.Drawing.Color.FromArgb(r, g, b);
- e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='" + EvenColor + "';this.style.color='black'");
- e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='" + mousecolor + "';this.style.color='#000000'");
- }
- else if (num % 2 == 1)
- {
- e.Item.BackColor = System.Drawing.Color.White;
- e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='" + OddColor + "';this.style.color='black'");
- e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='" + mousecolor + "';this.style.color='#000000'");
- }
- }
- //填充DataGrid的空白数据行
- public void AddBlankItem(DataGrid dg, int height)
- {
- //奇数行颜色
- // OddColor = "#FFFFFF";
- //偶数行颜色
- // EvenColor = "#FFFBD6";
- int left = total - num;
- for (int r = 0; r < left; r++)
- {
- DataGridItem item = new DataGridItem(-1, -1, ListItemType.Item);
- item.Height = Unit.Pixel(height);
- if (num % 2 == 0)
- {
- if (r % 2 == 0)
- {
- item.BackColor = System.Drawing.Color.FromName(EvenColor);
- }
- else if (r % 2 == 1)
- {
- item.BackColor = System.Drawing.Color.White;
- }
- }
- else if (num % 2 == 1)
- {
- if (r % 2 == 0)
- {
- item.BackColor = System.Drawing.Color.White;
- }
- else if (r % 2 == 1)
- {
- item.BackColor = System.Drawing.Color.FromName(EvenColor);
- }
- }
- //生成单元格
- for (int c = 0; c < cellcount; c++)
- {
- TableCell cell1 = new TableCell();
- cell1.Text = " ";
- item.Cells.Add(cell1);
- }
- //将生成的单元格添加到datagrid中
- dg.Controls[0].Controls.AddAt(num + 2 + r, item);
- }
- }
- /*=====================================定义GridView的背景色,和鼠标经过时的颜色===============================*/
- /// <summary>
- /// 鼠标经过时的颜色,行的颜色变化设置
- /// </summary>
- /// <param name="gv"></param>
- /// <param name="e"></param>
- public void SetGridView(GridView gv, GridViewRowEventArgs e, int height)
- {
- e.Row.Height = Unit.Pixel(height);
- int r = convert(EvenColor, 1, 2);
- int g = convert(EvenColor, 3, 2);
- int b = convert(EvenColor, 5, 2);
- if (num % 2 == 0)
- {
- //注:此处的颜色值只能用rgb三色值,不能用16位的值,不知为何,16位无法显示
- e.Row.BackColor = System.Drawing.Color.FromArgb(r, g, b);
- e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + EvenColor + "';this.style.color='black'");
- e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='" + mousecolor + "';this.style.color='#000000'");
- //e.Row.Attributes.Add("onmouseover", "this.style.background='url(../images/4.gif)';this.style.color='#ffffff'");
- }
- else if (num % 2 == 1)
- {
- e.Row.BackColor = System.Drawing.Color.White;
- e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='" + OddColor + "';this.style.color='black'");
- e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='" + mousecolor + "';this.style.color='#000000'");
- }
- }
- //添加空白数据行
- public void AddBlankRow(GridView gv, int height)
- {
- int left = total - num;
- for (int r = 0; r < left; r++)
- {
- GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal);
- row.Height = Unit.Pixel(height);
- if (num % 2 == 0)
- {
- if (r % 2 == 0)
- {
- row.BackColor = System.Drawing.Color.FromName(EvenColor);
- }
- else if (r % 2 == 1)
- {
- row.BackColor = System.Drawing.Color.White;
- }
- }
- else if (num % 2 == 1)
- {
- if (r % 2 == 0)
- {
- row.BackColor = System.Drawing.Color.White;
- }
- else if (r % 2 == 1)
- {
- row.BackColor = System.Drawing.Color.FromName(EvenColor);
- }
- }
- for (int c = 0; c < cellcount; c++)
- {
- TableCell cell1 = new TableCell();
- cell1.Text = " ";
- row.Cells.Add(cell1);
- }
- if (num != 0)
- {
- gv.Controls[0].Controls.AddAt(num + 2 + r, row);
- }
- else if (num == 0)
- {
- gv.Controls[0].Controls.AddAt(num + 1 + r, row);
- }
- }
- }
- }
四,datagrid填充
最新推荐文章于 2020-07-21 11:53:16 发布