怎样改变DataGrid中某一行的颜色 有时DataGrid中的某一行或几行要突出显示,需要此行与众不同,比如背景设或字体的不同,应该怎样实现呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskformattingthedatagridatdesigntime.asp 在ItemDataBound中 if ()//加条件{ for (int i=0;dg.items.count;i++) { e.Item.Cells[i].ForeColor = Color.Red } next} 你的改变颜色是什么意思?是指鼠标悬停在上面就改变一行的颜色吗?如果这样我这有一些代码private void DataGrid1_ItemDataBound(object senderm,System.Web.UI.WebControls.DataGridIteEventArgs e){if(E.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemTye.AlternatingItem){e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");e}} sorry ,上面的dg.item.count 改为:e.item.cells.count datagrid.item[x].cells.count可以设置这个属性的值 重写一个public class DataGridColoredTextBoxColumn:DataGridTextBoxColumnprotected 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){object o = this.GetColumnValueAtRow(source, rowNum); if( o!= null) { foreBrush = new SolidBrush(Color.Red); }} 在DataGrid的DataGridTableStyle中的DataGridColumnStyle 引用上面的DataGridColoredTextBoxColumn qwersky(爱睡觉的小虫):可能还没有理解我的意思,你写的是“列”的与众不同,可是我想要的是“行”与众不同...比如:DataGrid有20行,我想让第1、2、9、15行的背景色为红色,应该怎么写? 在ItemDataBount事件中,设置~!如:if(e.Item.Cell[0].Text=="1"){ e.Item.BackColor="red";} Sorry,上面的DataGrid是Windows控件,而不是Web控件。Windows中的DataGrid控件好象没有ItemDataBount事件 WPF下BitmapImage导致的内存泄露. c#编程实现生成圆弧的逐点比较法算法 如果一个委托中委托了多个函数,执行该委托时如何确定执行其中的一个? 新手问题 [assembly: AssemblyCulture("")] 问题 求高手解决 c#打印问题 求教正则表达式问题 如何使在DateTable中删除的记录能同步到数据源中? Visual C# 2005 Express中没有连接到SQLSERVER选项 请问关于C#对数据库操作的问题! 关于多线程的简单问题 dataGrid 中出现数组越界的 问题(向高手请教)
if ()//加条件
{
for (int i=0;dg.items.count;i++)
{
e.Item.Cells[i].ForeColor = Color.Red
}
next
}
{
if(E.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemTye.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
e
}
}
可以设置这个属性的值
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)
{
object o = this.GetColumnValueAtRow(source, rowNum);
if( o!= null)
{
foreBrush = new SolidBrush(Color.Red);
}
}
比如:DataGrid有20行,我想让第1、2、9、15行的背景色为红色,应该怎么写?
如:
if(e.Item.Cell[0].Text=="1")
{
e.Item.BackColor="red";
}