ultraGrid根据条件单元格背景变色
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace WindowsDataGrid
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("GPNO", typeof(string));
dataTable.Columns.Add("Price", typeof(float));
dataTable.Columns.Add("Price2", typeof(float));
dataTable.Rows.Add(new object[] { "54545", 1.02, -2.32 });
dataTable.Rows.Add(new object[] { "54522", -2.02, 1.2 });
dataTable.Rows.Add(new object[] { "54511", 3.02, -1.12 });
dataTable.Rows.Add(new object[] { "54512", 1.02, -2.32 });
dataTable.Rows.Add(new object[] { "54514", -2.02, 1.2 });
dataTable.Rows.Add(new object[] { "54513", 3.02, -1.12 });
ultraGrid1.DataSource = dataTable;
ultraGrid1.DataBind();
}
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
UltraGridLayout layout = e.Layout;
UltraGridBand rootBand = layout.Bands[0];
UltraGridOverride rootOverride = layout.Override;
rootBand.Columns["Price"].ValueBasedAppearance = CreateOperatorConditionValueAppearance(0);
rootBand.Columns["Price2"].ValueBasedAppearance = CreateOperatorConditionValueAppearance(0);
}
#region CreateOperatorConditionValueAppearance
/// <summary>
/// 根据数据值单元格动态变色
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
private ConditionValueAppearance CreateOperatorConditionValueAppearance(float num)
{
ConditionValueAppearance conditionValueAppearance = new ConditionValueAppearance();
OperatorCondition negativeCondition = new OperatorCondition(ConditionOperator.LessThan, num);
Infragistics.Win.Appearance negativeAppearance = new Infragistics.Win.Appearance("Negative");
negativeAppearance.ForeColor = Color.Green;
OperatorCondition positiveCondition = new OperatorCondition(ConditionOperator.GreaterThanOrEqualTo, num);
Infragistics.Win.Appearance positiveAppearance = new Infragistics.Win.Appearance("Positive");
positiveAppearance.ForeColor = Color.Red;
conditionValueAppearance.Add(negativeCondition, negativeAppearance);
conditionValueAppearance.Add(positiveCondition, positiveAppearance);
return conditionValueAppearance;
}
#endregion CreateOperatorConditionValueAppearance
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace WindowsDataGrid
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("GPNO", typeof(string));
dataTable.Columns.Add("Price", typeof(float));
dataTable.Columns.Add("Price2", typeof(float));
dataTable.Rows.Add(new object[] { "54545", 1.02, -2.32 });
dataTable.Rows.Add(new object[] { "54522", -2.02, 1.2 });
dataTable.Rows.Add(new object[] { "54511", 3.02, -1.12 });
dataTable.Rows.Add(new object[] { "54512", 1.02, -2.32 });
dataTable.Rows.Add(new object[] { "54514", -2.02, 1.2 });
dataTable.Rows.Add(new object[] { "54513", 3.02, -1.12 });
ultraGrid1.DataSource = dataTable;
ultraGrid1.DataBind();
}
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
{
UltraGridLayout layout = e.Layout;
UltraGridBand rootBand = layout.Bands[0];
UltraGridOverride rootOverride = layout.Override;
rootBand.Columns["Price"].ValueBasedAppearance = CreateOperatorConditionValueAppearance(0);
rootBand.Columns["Price2"].ValueBasedAppearance = CreateOperatorConditionValueAppearance(0);
}
#region CreateOperatorConditionValueAppearance
/// <summary>
/// 根据数据值单元格动态变色
/// </summary>
/// <param name="num"></param>
/// <returns></returns>
private ConditionValueAppearance CreateOperatorConditionValueAppearance(float num)
{
ConditionValueAppearance conditionValueAppearance = new ConditionValueAppearance();
OperatorCondition negativeCondition = new OperatorCondition(ConditionOperator.LessThan, num);
Infragistics.Win.Appearance negativeAppearance = new Infragistics.Win.Appearance("Negative");
negativeAppearance.ForeColor = Color.Green;
OperatorCondition positiveCondition = new OperatorCondition(ConditionOperator.GreaterThanOrEqualTo, num);
Infragistics.Win.Appearance positiveAppearance = new Infragistics.Win.Appearance("Positive");
positiveAppearance.ForeColor = Color.Red;
conditionValueAppearance.Add(negativeCondition, negativeAppearance);
conditionValueAppearance.Add(positiveCondition, positiveAppearance);
return conditionValueAppearance;
}
#endregion CreateOperatorConditionValueAppearance
}
}