一个winform程序,
将dataSet绑定给dataGrid后,我想设置:当字段1的值>100时,这笔记录的文字自动显示为红色,否则默认为黑色,代码如何写呢?
谢谢!!!

解决方案 »

  1.   

    to: 3000sunqin(3000suqnin)
        谢谢! 期待您的解答…
      

  2.   

    1、继承DataGridTextBoxColumn
    2、重写Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
    3、在Paint函数绘制控件前触发自定义事件
    4、在GRID事件响应函数里添加处理(如:当字段1的值>100时)
    5、根据判断结果设置参数
    6、在Paint函数根据5步设置的参数绘制绘制:
    if(不需要)
    base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
    else
    {
    g.FillRectangle(...);
    g.DrawString(...);
    }
      

  3.   

    这是测试代码;自己调试一下,基本原理和idiotzeng说的差不多;
    using System.Data;public class Form2 : System.Windows.Forms.Form
    {
     public Form2()
     {
      //
      // Windows 窗体设计器支持所必需的
      //
      InitializeComponent();  DataTable dTable;  //创建测试DataTable
      dTable = new DataTable("person");
      //添加测试列
      dTable.Columns.Add(new DataColumn("id",typeof(int)) );
      dTable.Columns.Add(new DataColumn("name",typeof(string)) );
      dTable.Columns.Add(new DataColumn("age",typeof(int)) );
      //添加测试行   
      DataRow newDataRow;
      Random iRandom = new Random();
      for(short i=0;i<20;i++)
      {
       newDataRow = dTable.NewRow();
       newDataRow[0] = i;
       newDataRow[1] = Convert.ToString(iRandom.Next(0,15))+"song";
       newDataRow[2] = iRandom.Next(15,30);
       dTable.Rows.Add(newDataRow);
      }  //设置列以及每列的行样式
      DataGridTableStyle dgTableStyle = new DataGridTableStyle();
      dgTableStyle.MappingName = dTable.TableName;
      foreach (DataColumn eachCol in dTable.Columns)
      {
       DataGridColoredTextBoxColumn dbColumnStyle = new DataGridColoredTextBoxColumn();
       dbColumnStyle.MappingName = eachCol.ColumnName;
       //设置变色行的条件以及前景和背景色
       dbColumnStyle.SetRowsColor(dTable.Select("age>20"), new SolidBrush(Color.White), new SolidBrush(Color.BlueViolet));
       dgTableStyle.GridColumnStyles.Add(dbColumnStyle);
      }
      
      //添加样式到dataGrid1
      dataGrid1.TableStyles.Add(dgTableStyle);
      
      dataGrid1.SetDataBinding(dTable,"");  
     }
    }public class  DataGridColoredTextBoxColumn : DataGridTextBoxColumn

     private System.Data.DataRow[] m_coloredDataRows;
     private System.Drawing.Brush m_foreBrush;
     private System.Drawing.Brush m_backBrush; //设置行色彩
     public void SetRowsColor(DataRow[] aDataRows, System.Drawing.Brush aForeBrush, System.Drawing.Brush aBackBrush)
     {
      //设置要改变颜色的行
      m_coloredDataRows = aDataRows;
      //设置前景色
      m_foreBrush = aForeBrush;
      //设置背景色
      m_backBrush = aBackBrush;
     } //重载绘制单元格的函数,如果该单元格所在行符合ColoredView中的条件,就改变前景和背景色
     protected override void Paint(System.Drawing.Graphics g, 
      System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager 
      source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush 
      foreBrush, bool alignToRight) 
     { 
      //获取当前正在显示的DataView
      DataView currentView;
      Type iType = this.DataGridTableStyle.DataGrid.DataSource.GetType();
      if (iType==typeof(System.Data.DataView) )
       currentView = (DataView)(this.DataGridTableStyle.DataGrid.DataSource);
      else if (iType==typeof(System.Data.DataTable) )
       currentView = ((DataTable)(this.DataGridTableStyle.DataGrid.DataSource)).DefaultView;
      else if (iType==typeof(System.Data.DataSet) )
       currentView = ((DataSet)(this.DataGridTableStyle.DataGrid.DataSource)).Tables[this.DataGridTableStyle.MappingName].DefaultView;
      else if (iType==typeof(System.Data.DataViewManager) )
      {
       DataViewManager viewManager = (DataViewManager)(this.DataGridTableStyle.DataGrid.DataSource);
       currentView = viewManager.DataSet.Tables[this.DataGridTableStyle.MappingName].DefaultView;
      }
      else
      { //如果dataSource不符合以上四种类型(应该不会吧^_^),抛出系统错误
       throw(new SystemException()); 
      }  //搜索当前行是否在设置行色彩的ColoredView当中
      DataRow currentRow = currentView[rowNum].Row;
      foreach (DataRow coloredRow in m_coloredDataRows)
      {
       if (currentRow == coloredRow) 
       {
        backBrush = m_backBrush;
        foreBrush = m_foreBrush; 
        break;
       }
      }  //调用基类的绘制函数
      base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
     } } 
      

  4.   

    private void datagridShow_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType!=ListItemType.Header)//鼠标移过效果
    {
    e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=\""+e.Item.Style["BACKGROUND-COLOR"]+"\"" );
    e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=\""+ "InactiveCaptionText"+"\"" );
    }
    if(e.Item.Cells[5].Text.ToString()=="1" & e.Item.ItemType!=ListItemType.Header)//文字变色显示
    {
    e.Item.Cells[5].ForeColor=System.Drawing.Color.Blue;
    }
                                if(e.Item.Cells[5].Text.ToString()=="2")
    {
    e.Item.Cells[5].ForeColor=System.Drawing.Color.YellowGreen;
    }
    }