100分请教, c# WinForm 如何运行时候,改变DataGrid 的字体背景色(依据数据值的不同)

ltmb118 2006-10-29 10:19:31

如题
...全文
437 19 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuzhlhua 2006-11-03
  • 打赏
  • 举报
回复
mark
woshibai112 2006-11-03
  • 打赏
  • 举报
回复
我也想知道 顶了
ltmb118 2006-11-02
  • 打赏
  • 举报
回复
solitude119()

这个有属性可以改的不是吗?
点 AlternatingRowsDefaulCellStyle 里面可以直接改的啊 背景色和 字体颜色都可以改的


------------------

需要运行时的
alias88 2006-11-02
  • 打赏
  • 举报
回复
你必需自己写DataGridColumnStyle ,并重写paint,然后你要做的事就多啦
例如不单可以动态绘制单元格不同的颜色,还可以绘制文本,甚至用不同的文本来代替显示,文本显示不下时会自动在最后加三个点...



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 );
}
}
}
ltmb118 2006-11-02
  • 打赏
  • 举报
回复
jedliu(21世纪什么最贵? 人才!)
大于100的什么颜色,小于10的什么颜色
-----------
类似这样的,希望给一段WinForm 的示例代码,谢谢
jedliu 2006-11-02
  • 打赏
  • 举报
回复
依据数据值的不同
----------------
根据数据的什么不同之处来决定颜色呢?总得有个标准吧?
比如,大于100的什么颜色,小于10的什么颜色!
如果是这样,那就在绑定的时候判断数值,然后在控件的样式里添加新的子对象。
路人霆 2006-11-02
  • 打赏
  • 举报
回复
Up
yuanzhihua520 2006-11-02
  • 打赏
  • 举报
回复
UP
steven262 2006-10-31
  • 打赏
  • 举报
回复
不会,帮你顶
lzpsky 2006-10-31
  • 打赏
  • 举报
回复
用datagrid的paint重新绘制Drawstring
yzqlee 2006-10-31
  • 打赏
  • 举报
回复
不如直接用第三方控件,好看又省事,直接改属性就OK
solitude119 2006-10-31
  • 打赏
  • 举报
回复
这个有属性可以改的不是吗?
点 AlternatingRowsDefaulCellStyle 里面可以直接改的啊 背景色和 字体颜色都可以改的
灰太狼 2006-10-31
  • 打赏
  • 举报
回复
override paint函數.
請參見http://www.syncfusion.com/FAQ/WindowsForms/中的datagird部分
lxmfll2000 2006-10-31
  • 打赏
  • 举报
回复
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;


}


}


}
ltmb118 2006-10-31
  • 打赏
  • 举报
回复
不如直接用第三方控件,好看又省事,直接改属性就OK
--------------------------------------------
那里可以下,免费的 ?
股神 2006-10-31
  • 打赏
  • 举报
回复
up
lxmfll2000 2006-10-30
  • 打赏
  • 举报
回复
你的意思是 改变第某行,某列的字体的颜色吧!
Knight94 2006-10-29
  • 打赏
  • 举报
回复
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
tete 2006-10-29
  • 打赏
  • 举报
回复
不会,帮你顶 吧

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧