如何改变DataGrid的任意一个cell的颜色

starkey 2003-08-05 10:27:33
别告诉我要什么DataGridStyle,因为我是DataGrid直接绑定一张DataTable,根本没有用到DataGridStyle。
多谢多谢,253,253
...全文
42 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ani 2003-08-05
  • 打赏
  • 举报
回复
up
myhero811104 2003-08-05
  • 打赏
  • 举报
回复
WebForm中
DataGrid1.Items[i].Cells[j].BackColor = Color.Red;
i,j 是第几行、第几列
myhero811104 2003-08-05
  • 打赏
  • 举报
回复
WebForm中
DataGrid1.Items[i].Cells[j].BackColor = Color.Red;
i,j 是第几行、第几列
ddy2000 2003-08-05
  • 打赏
  • 举报
回复
up
starkey 2003-08-05
  • 打赏
  • 举报
回复
楼上的这样做法我看到过,好像要把DataGridColoredTextBoxColumn添加到DataGridStyle里面的。
我说过,我是DataGrid直接绑定DataTable,根本没用到什么DataGridSytle。
而且楼上的这种做法只能处理一列,好像两列就没办法了。
因为对于不同的列,paint是一样的,但我用来判断的数据格式就有可能不一样。
另外我的一张DataGrid绑定了不同的DataTable,用上面的方法恐怕会非常非常麻烦。
雪狼1234567 2003-08-05
  • 打赏
  • 举报
回复
根据值得的不同显示不同的颜色
public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn

{

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)

{

// the idea is to conditionally set the foreBrush and/or backbrush

// depending upon some crireria on the cell value

// Here, we color anything that begins with a letter higher than 'F'

try{

object o = this.GetColumnValueAtRow(source, rowNum);

if( o!= null)

{

char c = ((string)o)[0];

if( c > 'F')

{

// could be as simple as

// backBrush = new SolidBrush(Color.Pink);

// or something fancier...

backBrush = new LinearGradientBrush(bounds,

Color.FromArgb(255, 200, 200),

Color.FromArgb(128, 20, 20),

LinearGradientMode.BackwardDiagonal);

foreBrush = new SolidBrush(Color.White);

}

}

}

catch(Exception ex){ /* empty catch */ }

finally{

// make sure the base class gets called to do the drawing with

// the possibly changed brushes

base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);

}

}

}

brightheroes 2003-08-05
  • 打赏
  • 举报
回复
www.C-sharpcorner.com
search datagrid
or
http://www.syncfusion.com/FAQ/WinForms/
starkey 2003-08-05
  • 打赏
  • 举报
回复
继续up,没有人知道吗
我C#用的时间不长,C#里还有句柄这个概念吗
liduke(天下有雪) 能不能说具体点
qimeizhi 2003-08-05
  • 打赏
  • 举报
回复
to heroux(FlyerAero):
System.Windows.Forms.DataGrid 没有items属性,System.web中倒是有,只是我们现在做的时窗体应用程序!
heroux 2003-08-05
  • 打赏
  • 举报
回复
用得着大家写的那么麻烦吗?
如下:
DataGrid1.Items[1].Cells[1].Attributes["style"]="COLOR: #ff00ff";
qimeizhi 2003-08-05
  • 打赏
  • 举报
回复
我也遇到类似的问题,关注中~!!!
http://expert.csdn.net/Expert/topic/2110/2110057.xml?temp=.1420404
liduke 2003-08-05
  • 打赏
  • 举报
回复
得到当前cell的句柄,就可以通过它的属性来对它的着色进行处理了
MarcuseXiao 2003-08-05
  • 打赏
  • 举报
回复
up

110,539

社区成员

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

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

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