How do I color a individual cell depending upon its value or some external method? http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q
namespace DataGridCheckBox { using System; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; public class Form1 : System.Windows.Forms.Form { private System.ComponentModel.Container components; private DataGrid myDataGrid; private DataSet myDataSet; private bool TablesAlreadyAdded;
public Form1() { // Required for Windows Form Designer support. InitializeComponent(); // Call SetUp to bind the controls. SetUp(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose();} } base.Dispose( disposing ); } private void InitializeComponent() { this.myDataGrid = new System.Windows.Forms.DataGrid(); ((System.ComponentModel.ISupportInitialize)(this.myDataGrid)).BeginInit(); this.SuspendLayout(); // // myDataGrid // this.myDataGrid.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.myDataGrid.CaptionText = "Microsoft DataGrid Control"; this.myDataGrid.DataMember = ""; this.myDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.myDataGrid.Location = new System.Drawing.Point(72, 48); this.myDataGrid.Name = "myDataGrid"; this.myDataGrid.Size = new System.Drawing.Size(312, 200); this.myDataGrid.TabIndex = 2; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(450, 301); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.myDataGrid}); this.Name = "Form1"; this.Text = "DataGrid Control Sample"; ((System.ComponentModel.ISupportInitialize)(this.myDataGrid)).EndInit(); this.ResumeLayout(false); } public static void Main() { Application.Run(new Form1()); }
private void SetUp() { // Create a DataSet with two tables and one relation. MakeDataSet(); /* Bind the DataGrid to the DataSet. The dataMember specifies that the Customers table should be displayed.*/ myDataGrid.SetDataBinding(myDataSet, "EastCoastSales"); //create and add a custom table style so we can //easily get at the behavior of a cell... AddCustomDataTableStyle(); } private void AddCustomDataTableStyle() { //STEP 1: Create a DataTable style object and set properties if required. DataGridTableStyle ts1 = new DataGridTableStyle(); //specify the table from dataset (required step) ts1.MappingName = "EastCoastSales";
// Set other properties (optional step) ts1.AlternatingBackColor = Color.LightBlue; //STEP 2: Create a string column and add it to the tablestyle DataGridColumnStyle TextCol = new DataGridTextBoxColumn(); TextCol.MappingName = "custName"; //from dataset table TextCol.HeaderText = "Customer Name"; TextCol.Width = 100; ts1.GridColumnStyles.Add(TextCol); //STEP 3: Create an int column style and add it to the tablestyle //this requires setting the format for the column through its property descriptor PropertyDescriptorCollection pdc = this.BindingContext [myDataSet, "EastCoastSales"].GetItemProperties(); //now created a formated column using the pdc DataGridTextBoxColumn csIDInt = new DataGridTextBoxColumn(pdc["CustID"], "i", true); csIDInt.MappingName = "CustID"; csIDInt.HeaderText = "CustID"; csIDInt.Width = 50; ts1.GridColumnStyles.Add(csIDInt); //STEP 4: Add the checkbox DataGridColumnStyle boolCol = new DataGridBoolColumn(); boolCol.MappingName = "Current"; boolCol.HeaderText = "Info Current"; boolCol.Width = 70; ts1.GridColumnStyles.Add(boolCol); //STEP 5: Add the tablestyle to your datagrid抯 tablestlye collection myDataGrid.TableStyles.Add(ts1);
} // Create a DataSet with two tables and populate it. private void MakeDataSet() { // Create a DataSet. myDataSet = new DataSet("myDataSet");
// Create two DataTables. DataTable tCust = new DataTable("EastCoastSales");
// Create two columns, and add them to the first table. DataColumn cCustID = new DataColumn("CustID", typeof(int)); DataColumn cCustName = new DataColumn("CustName"); DataColumn cCurrent = new DataColumn("Current", typeof(bool)); tCust.Columns.Add(cCustID); tCust.Columns.Add(cCustName); tCust.Columns.Add(cCurrent); // Add the tables to the DataSet. myDataSet.Tables.Add(tCust);
/* Populates the tables. For each customer and order, creates two DataRow variables. */ DataRow newRow1;
// Create three customers in the Customers Table. for(int i = 1; i < 4; i++) { newRow1 = tCust.NewRow(); newRow1["custID"] = i; // Add the row to the Customers table. tCust.Rows.Add(newRow1); } // Give each customer a distinct name. tCust.Rows[0]["custName"] = "Alpha"; tCust.Rows[1]["custName"] = "Beta"; tCust.Rows[2]["custName"] = "Omega"; // Give the Current column a value. tCust.Rows[0]["Current"] = true; tCust.Rows[1]["Current"] = true; tCust.Rows[2]["Current"] = false; } } }
http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q745q
{
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms; public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private DataGrid myDataGrid;
private DataSet myDataSet;
private bool TablesAlreadyAdded;
public Form1()
{
// Required for Windows Form Designer support.
InitializeComponent();
// Call SetUp to bind the controls.
SetUp();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.myDataGrid = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.myDataGrid)).BeginInit();
this.SuspendLayout();
//
// myDataGrid
//
this.myDataGrid.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.myDataGrid.CaptionText = "Microsoft DataGrid Control";
this.myDataGrid.DataMember = "";
this.myDataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.myDataGrid.Location = new System.Drawing.Point(72, 48);
this.myDataGrid.Name = "myDataGrid";
this.myDataGrid.Size = new System.Drawing.Size(312, 200);
this.myDataGrid.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(450, 301);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.myDataGrid});
this.Name = "Form1";
this.Text = "DataGrid Control Sample";
((System.ComponentModel.ISupportInitialize)(this.myDataGrid)).EndInit();
this.ResumeLayout(false); } public static void Main()
{
Application.Run(new Form1());
}
private void SetUp()
{
// Create a DataSet with two tables and one relation.
MakeDataSet();
/* Bind the DataGrid to the DataSet. The dataMember
specifies that the Customers table should be displayed.*/
myDataGrid.SetDataBinding(myDataSet, "EastCoastSales"); //create and add a custom table style so we can
//easily get at the behavior of a cell...
AddCustomDataTableStyle();
} private void AddCustomDataTableStyle()
{
//STEP 1: Create a DataTable style object and set properties if required.
DataGridTableStyle ts1 = new DataGridTableStyle(); //specify the table from dataset (required step)
ts1.MappingName = "EastCoastSales";
// Set other properties (optional step)
ts1.AlternatingBackColor = Color.LightBlue; //STEP 2: Create a string column and add it to the tablestyle
DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "custName"; //from dataset table
TextCol.HeaderText = "Customer Name";
TextCol.Width = 100;
ts1.GridColumnStyles.Add(TextCol); //STEP 3: Create an int column style and add it to the tablestyle
//this requires setting the format for the column through its property descriptor
PropertyDescriptorCollection pdc = this.BindingContext
[myDataSet, "EastCoastSales"].GetItemProperties(); //now created a formated column using the pdc
DataGridTextBoxColumn csIDInt =
new DataGridTextBoxColumn(pdc["CustID"], "i", true);
csIDInt.MappingName = "CustID";
csIDInt.HeaderText = "CustID";
csIDInt.Width = 50;
ts1.GridColumnStyles.Add(csIDInt); //STEP 4: Add the checkbox
DataGridColumnStyle boolCol = new DataGridBoolColumn();
boolCol.MappingName = "Current";
boolCol.HeaderText = "Info Current";
boolCol.Width = 70;
ts1.GridColumnStyles.Add(boolCol);
//STEP 5: Add the tablestyle to your datagrid抯 tablestlye collection
myDataGrid.TableStyles.Add(ts1);
}
// Create a DataSet with two tables and populate it.
private void MakeDataSet()
{
// Create a DataSet.
myDataSet = new DataSet("myDataSet");
// Create two DataTables.
DataTable tCust = new DataTable("EastCoastSales");
// Create two columns, and add them to the first table.
DataColumn cCustID = new DataColumn("CustID", typeof(int));
DataColumn cCustName = new DataColumn("CustName");
DataColumn cCurrent = new DataColumn("Current", typeof(bool));
tCust.Columns.Add(cCustID);
tCust.Columns.Add(cCustName);
tCust.Columns.Add(cCurrent); // Add the tables to the DataSet.
myDataSet.Tables.Add(tCust);
/* Populates the tables. For each customer and order,
creates two DataRow variables. */
DataRow newRow1;
// Create three customers in the Customers Table.
for(int i = 1; i < 4; i++)
{
newRow1 = tCust.NewRow();
newRow1["custID"] = i;
// Add the row to the Customers table.
tCust.Rows.Add(newRow1);
}
// Give each customer a distinct name.
tCust.Rows[0]["custName"] = "Alpha";
tCust.Rows[1]["custName"] = "Beta";
tCust.Rows[2]["custName"] = "Omega"; // Give the Current column a value.
tCust.Rows[0]["Current"] = true;
tCust.Rows[1]["Current"] = true;
tCust.Rows[2]["Current"] = false;
}
}
}
請參見http://www.syncfusion.com/FAQ/WindowsForms/中的datagird部分
点 AlternatingRowsDefaulCellStyle 里面可以直接改的啊 背景色和 字体颜色都可以改的
点 AlternatingRowsDefaulCellStyle 里面可以直接改的啊 背景色和 字体颜色都可以改的
------------------ 需要运行时的
大于100的什么颜色,小于10的什么颜色
-----------
类似这样的,希望给一段WinForm 的示例代码,谢谢
例如不单可以动态绘制单元格不同的颜色,还可以绘制文本,甚至用不同的文本来代替显示,文本显示不下时会自动在最后加三个点...public class myDataGridTextBoxColumn : DataGridColumnStyle
{ protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
string text1 = this.GetText(this.GetColumnValueAtRow(source, rowNum));
if (!this.DataGridTableStyle.DataGrid.IsSelected (rowNum))
{
System.Drawing .Color Forecolor=this.GetDynamicColor(,,)
System.Drawing .Color Backcolor=this.GetDynamicColor(,,);
foreBrush=new System.Drawing.SolidBrush(Forecolor);
backBrush=new System.Drawing.SolidBrush(Backcolor);
}
else
{
backBrush=new System.Drawing.SolidBrush(System.Drawing .Color .LightSlateGray );
}
this.PaintText(g, bounds, text1, backBrush, foreBrush, alignToRight);
} protected void PaintText(Graphics g, Rectangle textBounds, string text, Brush backBrush, Brush foreBrush, bool alignToRight)
{
try
{
Rectangle rectangle = textBounds;
StringFormat format = new StringFormat(StringFormat.GenericDefault ); if (alignToRight)
{
format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
format.FormatFlags|=StringFormatFlags.DisplayFormatControl ;
format.FormatFlags|=StringFormatFlags.MeasureTrailingSpaces ;
format.Trimming = StringTrimming.None ;
format.Trimming=StringTrimming.EllipsisCharacter ;
format.Alignment = (this.Alignment == HorizontalAlignment.Left) ? StringAlignment.Near : ((this.Alignment == HorizontalAlignment.Center) ? StringAlignment.Center : StringAlignment.Far); g.FillRectangle(backBrush, rectangle);
g.DrawString(text, this.DataGridTableStyle.DataGrid.Font, foreBrush, (RectangleF) rectangle, format);
format.Dispose();
}
catch(System.Exception ex)
{
System.Console.WriteLine ("在绘制单元格时发生错误:{0}",ex.Message );
}
}
}