有时DataGrid中的某一行或几行要突出显示,需要此行与众不同,比如背景设或字体的不同,应该怎样实现呢?

解决方案 »

  1.   

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskformattingthedatagridatdesigntime.asp
      

  2.   

    在ItemDataBound中
      if ()//加条件
    {
      for (int i=0;dg.items.count;i++)
           {
         e.Item.Cells[i].ForeColor = Color.Red
            }
        next
    }
      

  3.   

    你的改变颜色是什么意思?是指鼠标悬停在上面就改变一行的颜色吗?如果这样我这有一些代码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
    }
    }
      

  4.   

    sorry ,上面的dg.item.count 改为:e.item.cells.count
      

  5.   

    datagrid.item[x].cells.count
    可以设置这个属性的值
      

  6.   

    重写一个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)
    {
    object o = this.GetColumnValueAtRow(source, rowNum);
    if( o!= null)
    {
    foreBrush = new SolidBrush(Color.Red);

    }
    }
      

  7.   

    在DataGrid的DataGridTableStyle中的DataGridColumnStyle 引用上面的DataGridColoredTextBoxColumn
      

  8.   

    qwersky(爱睡觉的小虫):可能还没有理解我的意思,你写的是“列”的与众不同,可是我想要的是“行”与众不同...
    比如:DataGrid有20行,我想让第1、2、9、15行的背景色为红色,应该怎么写?
      

  9.   

    在ItemDataBount事件中,设置~!
    如:
    if(e.Item.Cell[0].Text=="1")
    {
         e.Item.BackColor="red";
    }
      

  10.   

    Sorry,上面的DataGrid是Windows控件,而不是Web控件。Windows中的DataGrid控件好象没有ItemDataBount事件