【Winform】devexpress控件中的gridControl设置某个单元格属性。

a12321321321312321 2010-11-10 02:29:46
RT。。要设置gridControl中gridview中某个单元格的字体样式是红色显示。
需要说明的是gridControl中的数据源datatable是3秒钟刷新一次,所以只能在 datatable的RowChanged 里面写代码。
在网上找了一个设置单元格样式的例子是在RowCellStyle事件里面写代码。RowCellStyle是创建哪一行时候才触发所以不行。

http://www.devexpress.com/Support/Center/KB/p/A255.aspx


我现在只能实现让他的某一列样式改变。不知道怎么设置它gridview的某个单元格的属性。



我的实现代码。


DataSet ds = new DataSet();


void aa()
{
while (true)
{
ds.Tables[1].Rows[0][1] = (int.Parse(ds.Tables[1].Rows[0][1].ToString()) + 1).ToString();
Thread.Sleep(3000);
}
}

private void XtraForm1_Load(object sender, EventArgs e)
{
ds.Tables.Add("dt1");
ds.Tables.Add("dt2");
ds.Tables.Add("dt3");
//-----------------------------------------
ds.Tables[0].Columns.Add("dt1aa");
ds.Tables[0].Columns.Add("dt1bb");
DataRow dr = ds.Tables[0].NewRow();
dr[0] = 0;
dr[1] = 1;
ds.Tables[0].Rows.Add(dr);
dr = ds.Tables[0].NewRow();
dr[0] = 2;
dr[1] = 3;
ds.Tables[0].Rows.Add(dr);
//-------------------------------------
ds.Tables[1].Columns.Add("dt2aa");
ds.Tables[1].Columns.Add("dt2bb");
dr = ds.Tables[1].NewRow();
dr[0] = 0;
dr[1] = 1;
ds.Tables[1].Rows.Add(dr);
dr = ds.Tables[1].NewRow();
dr[0] = 0;
dr[1] = 1;
ds.Tables[1].Rows.Add(dr);
//-------------------------------------
ds.Tables[2].Columns.Add("dt3aa");
ds.Tables[2].Columns.Add("dt3bb");
dr = ds.Tables[2].NewRow();
dr[0] = 0;
dr[1] = 1;
ds.Tables[2].Rows.Add(dr);
dr = ds.Tables[2].NewRow();
dr[0] = 0;
dr[1] = 1;
ds.Tables[2].Rows.Add(dr);

ds.Relations.Add("test1", ds.Tables[0].Columns["dt1aa"], ds.Tables[1].Columns["dt2aa"]);
ds.Relations.Add("test2", ds.Tables[0].Columns["dt1aa"], ds.Tables[2].Columns["dt3aa"]);
gridControl1.DataSource = ds.Tables[0];
gridView1.Columns[0].Width = 150;
ds.Tables[1].RowChanged += new DataRowChangeEventHandler(XtraForm1_RowChanged);
Thread t = new Thread(new ThreadStart(aa));
t.Start();
}


public delegate void setCellColor(GridView gv, int Col);

void SetColor(GridView gv,int Col)
{
if (Col == 1)
{
gv.Columns[1].AppearanceCell.ForeColor = Color.Red;
}
else
{
gv.Columns[1].AppearanceCell.ForeColor = Color.Black;
}


}

void XtraForm1_RowChanged(object sender, DataRowChangeEventArgs e)
{
GridView gv= gridView1.GetDetailView(0, 0) as GridView;

if (gv != null)
{

if (int.Parse(e.Row[1].ToString()) % 2 == 0)
{
this.BeginInvoke(new setCellColor(SetColor), new object[] { gv, 1 });

}
else
{
this.BeginInvoke(new setCellColor(SetColor), new object[] { gv, 0 });

}

}

}
...全文
2544 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
fanlvlgh 2013-04-09
  • 打赏
  • 举报
回复
引用 13 楼 f800051235 的回复:
引用 4 楼 f800051235 的回复:自己解决了,MD,哪个傻X说RowCellStyle是在创建那一行的时候执行。害的我走了一天的弯路。。。 test
test
a12321321321312321 2013-04-09
  • 打赏
  • 举报
回复
引用 4 楼 f800051235 的回复:
自己解决了,MD,哪个傻X说RowCellStyle是在创建那一行的时候执行。害的我走了一天的弯路。。。
test
罚罚啦 2013-03-21
  • 打赏
  • 举报
回复
非常感谢!!
江南小鱼 2012-09-05
  • 打赏
  • 举报
回复
GvShuiBiaoInfo.Columns["S_XINBIAOTXM"].OptionsColumn.AllowFocus = true;
GvShuiBiaoInfo.Columns["S_XINBIAOTXM"].OptionsColumn.AllowEdit = true;
a12321321321312321 2012-07-25
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

怎么解决的说下呢
[/Quote]
gridview 在RowCellStyle事件里面写代码。

cardView 在CustomDrawCardFieldValue写代码。

DEMO:
private void cardView2_CustomDrawCardFieldValue(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
try
{


DataRow drtemp = ((CardView)sender).GetDataRow(e.RowHandle);
if (drtemp != null)
{
if (你的条件)
{

e.Appearance.ForeColor = Color.Red;
}
hhdbinbin 2012-07-23
  • 打赏
  • 举报
回复
怎么解决的说下呢
hhdbinbin 2012-07-23
  • 打赏
  • 举报
回复
怎么解决的说下呢
xuzhenlei123 2012-05-24
  • 打赏
  • 举报
回复
解决了也不说下解决方法啊?
hongjiaoli 2010-11-11
  • 打赏
  • 举报
回复
200分全部都给了我,有点受宠若惊……
谢谢楼主。
lee1800 2010-11-10
  • 打赏
  • 举报
回复
请教下 如何根据索引取得某一单元格的属性
a12321321321312321 2010-11-10
  • 打赏
  • 举报
回复
自己解决了,MD,哪个傻X说RowCellStyle是在创建那一行的时候执行。害的我走了一天的弯路。。。
a12321321321312321 2010-11-10
  • 打赏
  • 举报
回复
DDDDDDDDDDDDD
a12321321321312321 2010-11-10
  • 打赏
  • 举报
回复
dataGridView1里面可以直接获得它单元格的样式。但是貌似gridcontrol下的gridview只能设置某行或者某列的样式。
if (int.Parse(dt.Rows[0][0].ToString()) % 2 == 0)
{
DataGridViewCellStyle styleI = new DataGridViewCellStyle();
styleI.ForeColor = Color.Red;

dataGridView1.Rows[0].Cells[0].Style = styleI;
}
else
{
DataGridViewCellStyle styleI = new DataGridViewCellStyle();
styleI.ForeColor = Color.Black;

dataGridView1.Rows[0].Cells[0].Style = styleI;
}

111,129

社区成员

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

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

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