System.Web.UI.CssStyleCollection css=new CssStyleCollection();
css.Add("ForeColor","#FF0000");
css.Add("BackColor","#E7E7FF");
if(this.DataGrid1.SelectedIndex==2)
{
this.DataGrid1.SelectedItem.Style=css;
}

解决方案 »

  1.   

    应该是这样:System.Web.UI.CssStyleCollection css=new CssStyleCollection();
    css.Add("ForeColor","#FF0000");
    css.Add("BackColor","#E7E7FF");
    foreach(DataGridItem dgi in DataGrid1.Items )
    {
    if(dgi.ItemIndex==2||dgi.ItemIndex==8)
    {
    DataGrid1.Items[dgi.ItemIndex].Style=css;
    }
    }
      

  2.   

    不错,看来对Css还要加深理解了!
      

  3.   

    也可以直接把值赋给Style属性:
    foreach(DataGridItem dgi in DataGrid1.Items )
       if(dgi.ItemIndex==2||dgi.ItemIndex==7||dgi.ItemIndex==8)
          DataGrid1.Items[dgi.ItemIndex].Style="background-color: #fcfcfc; color: #666666;";