标题:datagird根据条件改变多行颜色的问题,一天没搞定,求高人帮忙
取消只看楼主
tyg1013
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-4-29
 问题点数:0 回复次数:0 
datagird根据条件改变多行颜色的问题,一天没搞定,求高人帮忙
下面是一个例子,不过数据是别人自己添加的数组,希望改成从数据库读取数据的实例,但是本人不才改了一下午没搞定,希望高手帮改下,拜谢····
using System;
using System.Drawing;
using System.Collections;
using
using System.Windows.Forms;
using System.Data;
 
namespace DataGridBackColor
{
     ///<summary>
     /// BackColorForm 的摘要描述。
     ///</summary>
     public class BackColorForm : System.Windows.Forms.Form
     {
         //定义DataGridColoredTextBoxColumn类
         public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
         {
              //利用DataView来传递当前DataGrid中的数据
              public DataView dv = null;
 
              //重写Paint
              protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
              {
                  try
                  {
                       //判断DataGrid中每行第一列的数据是否为如下的字符串
                       switch( dv[rowNum].Row[0].ToString() )
                       {
                           case "critical" : backBrush = Brushes.Red; break;//改变颜色
                           case "major" : backBrush = Brushes.Brown; break;//改变颜色
                           case "minor" : backBrush = Brushes.Orange; break;//改变颜色
                           case "warning" : backBrush = Brushes.YellowGreen; break;//改变颜色
                           default : break;
                       }
                  }
                  catch( Exception ex )
                  {
                       MessageBox.Show( ex.Message );
                  }
                  finally
                  {
                       //调用基类的Paint方法
                       base.Paint (g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
                  }
              }
         }
         private System.Windows.Forms.DataGrid dataGrid;
         private System.Windows.Forms.DataGridTableStyle TableStyle;
         private DataGridColoredTextBoxColumn Status;
         private DataGridColoredTextBoxColumn TextBox1;
         private DataGridColoredTextBoxColumn TextBox2;
         
         ///<summary>
         ///设计工具所需的变数。
         ///</summary>
         private components = null;
 
         public BackColorForm()
         {
              //
              // Windows Form 设计工具支持的必要项
              //
              InitializeComponent();
 
              //
              // TODO: 在 InitializeComponent 呼叫之后加入任何建构函式程序代码
              //
         }
 
         ///<summary>
         ///清除任何使用中的资源。
         ///</summary>
         protected override void Dispose( bool disposing )
         {
              if( disposing )
              {
                  if(components != null)
                  {
                       components.Dispose();
                  }
              }
              base.Dispose( disposing );
         }
 
         #region Windows Form 设计工具产生的程序代码
         ///<summary>
         ///此为设计工具支持所必须的方法 - 请勿使用程序代码编辑器修改
         ///这个方法的内容。
         ///</summary>
         private void InitializeComponent()
         {
              this.dataGrid = new System.Windows.Forms.DataGrid();
              this.TableStyle = new System.Windows.Forms.DataGridTableStyle();
              this.Status = new DataGridColoredTextBoxColumn();
              this.TextBox1 = new DataGridColoredTextBoxColumn();
              this.TextBox2 = new DataGridColoredTextBoxColumn();
              (()(this.dataGrid)).BeginInit();
              this.SuspendLayout();
              //
              // dataGrid
              //
              this.dataGrid.CaptionText = "BackColorForm";
              this.dataGrid.DataMember = "";
              this.dataGrid.Dock = System.Windows.Forms.DockStyle.Fill;
              this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
              this.dataGrid.Location = new System.Drawing.Point(0, 0);
              this.dataGrid.Name = "dataGrid";
              this.dataGrid.Size = new System.Drawing.Size(292, 266);
              this.dataGrid.TabIndex = 0;
              this.dataGrid.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] {
                                                                                                               this.TableStyle});
              //
              // TableStyle
              //
              this.TableStyle.AlternatingBackColor = System.Drawing.Color.Wheat;
              this.TableStyle.BackColor = System.Drawing.Color.LightGray;
              this.TableStyle.DataGrid = this.dataGrid;
              this.TableStyle.ForeColor = System.Drawing.Color.MidnightBlue;
              this.TableStyle.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] {
                                                                                                                        this.Status,
                                                                                                                        this.TextBox1,
                                                                                                                        this.TextBox2});
              this.TableStyle.GridLineColor = System.Drawing.Color.Black;
              this.TableStyle.HeaderBackColor = System.Drawing.Color.LightBlue;
              this.TableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText;
              this.TableStyle.MappingName = "";
              this.TableStyle.SelectionBackColor = System.Drawing.Color.LightSkyBlue;
              this.TableStyle.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
              //
              // Status
              //
              this.Status.Format = "";
              this.Status.FormatInfo = null;
              this.Status.HeaderText = "Status";
              this.Status.MappingName = "";
              this.Status.NullText = "";
              this.Status.Width = 75;
              //
              // TextBox1
              //
              this.TextBox1.Format = "";
              this.TextBox1.FormatInfo = null;
              this.TextBox1.HeaderText = "TextBox1";
              this.TextBox1.MappingName = "";
              this.TextBox1.NullText = "";
              this.TextBox1.Width = 75;
              //
              // TextBox2
              //
              this.TextBox2.Format = "";
              this.TextBox2.FormatInfo = null;
              this.TextBox2.HeaderText = "TextBox2";
              this.TextBox2.MappingName = "";
              this.TextBox2.NullText = "";
              this.TextBox2.Width = 75;
              //
              // BackColorForm
              //
              this.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
              this.ClientSize = new System.Drawing.Size(292, 266);
              this.Controls.Add(this.dataGrid);
              this.Name = "BackColorForm";
              this.Text = "BackColorForm";
              this.Load += new System.EventHandler(this.Form1_Load);
              (()(this.dataGrid)).EndInit();
              this.ResumeLayout(false);
 
         }
         #endregion
 
         ///<summary>
         ///应用程序的主进入点。
         ///</summary>
         [STAThread]
         static void Main()
         {
              Application.Run(new BackColorForm());
         }
 
         #region User Variable
         private DataTable UserTable = null;
         private DataView UserView = null;
         #endregion
 
         private void Form1_Load(object sender, System.EventArgs e)
         {
              try
              {
                  InitDataTable();
                  InitDataView();
                  InitDataGrid();
                  InitData();
              }
              catch( Exception ex )
              {
                  MessageBox.Show( ex.Message );
              }
         }
         
         private void InitDataTable()
         {
              UserTable = new DataTable("UserTable");
              DataColumn[] d = new DataColumn[4];
              d[0] = new DataColumn("d0",typeof(string));
              d[1] = new DataColumn("d1",typeof(string));
              d[2] = new DataColumn("d2",typeof(string));
              UserTable.Columns.AddRange( d );
 
              TableStyle.MappingName = "UserTable";
              Status.MappingName = "d0";
              TextBox1.MappingName = "d1";
              TextBox2.MappingName = "d2";
         }
 
         private void InitDataView()
         {
              UserView = new DataView();
              UserView.Table = UserTable;
              UserView.AllowNew = false;
 
              //将DataView赋给DataGridColoredTextBoxColumn类中的DataView变量
              Status.dv = UserView;
              TextBox1.dv = UserView;
              TextBox2.dv = UserView;
         }
 
         private void InitDataGrid()
         {
              dataGrid.DataSource = UserView;
         }
 
         private void InitData()
         {
              DataRow r = null;
              //Row 1
              r = UserTable.NewRow();
              r[0] = "critical";
              r[1] = "TextBox11";
              r[2] = "TextBox21";
              UserTable.Rows.Add( r );
              //Row 2
              r = UserTable.NewRow();
              r[0] = "major";
              r[1] = "TextBox12";
              r[2] = "TextBox22";
              UserTable.Rows.Add( r );
              //Row 3
              r = UserTable.NewRow();
              r[0] = "minor";
              r[1] = "TextBox13";
              r[2] = "TextBox23";
              UserTable.Rows.Add( r );
              //Row 4
              r = UserTable.NewRow();
              r[0] = "warning";
              r[1] = "TextBox14";
              r[2] = "TextBox24";
              UserTable.Rows.Add( r );
              
         }
     }
}
希望改成数据库连接的形式,数据库的结构随便
例如:表:test1
a  b   c
搜索更多相关主题的帖子: using datagird System 数据库 高人 
2008-04-29 09:36



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-211600-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.153019 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved