比如dataset有3个字段,一个是文字,一个存的是颜色,一个是控制是文字居中还是靠左或靠右,
绑定到一个datagrid,如何让每一行 “文字” 的颜色是该行颜色字段的颜色,对齐方式是该行的对齐字段的内容。

解决方案 »

  1.   

    实现datagrid的ItemDataBound事件例:
    //ds是数据源private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemIndex>=0)
    {
    DataRow dr=ds.Tables[0].Rows[e.Item.DataSetIndex];
    e.Item.Cells[0].Text=String.Format("<div style='text-align:{2};color:{1}'>{0}</div>",
    dr["text"].ToString(),
    dr["color"].ToString(),
    dr["align"].ToString());
    }
    }
      

  2.   

    if(e.Item.Cells[0].Text>="60")
      {
        e.Item.Cells[1].ForeColor=System.Drawing.Color.red;
      }
    else
      {
        e.Item.Cells[1].ForeColor=System.Drawing.Color.Blue;
      }