2

我的 gridview 是从从 SQL 读取的 Datatable 动态生成的,现在我想设置一个特定的列,比如“salary”,使其具有红色背景色。我已经搜索了很多解决方案,但仍然没有任何线索。

CmdString = @"  select id, firstname, lastname, title, level, salary from emplpyees";
            SqlCommand cmd = new SqlCommand(CmdString, con);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            dt = new DataTable("employee");
            sda.Fill(dt);
            datagridall.ItemsSource = dt.DefaultView;
4

3 回答 3

2

试试下面的代码:

gvUserInfo.Columns[0].ItemStyle.BackColor = System.Drawing.Color.Red;

之后将相同的代码放在 BindGrid 函数中gvUserInfo.DataBind();

享受..

于 2013-07-26T12:19:22.200 回答
1

您可以创建一个转换器类

public class ErrorConverters : IValueConverter
  {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      SolidColorBrush myBrush = default(SolidColorBrush);

      if ((bool)value == true)
      {
        myBrush = new SolidColorBrush(Colors.Red);
      }
      else
      {
        myBrush = new SolidColorBrush(Colors.Black);
      }

      return myBrush;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      //Throw New NotImplementedException()
      return null;
    }
  }

然后使用行模板将背景颜色绑定到数据库中将传递给转换器的任何项目。

于 2013-07-25T22:15:30.607 回答
0

遵循此代码:

protected void gvBandA_RowDataBound(object sender, GridViewRowEventArgs e)
    {
     //   e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
        gvBandA.Columns[1].ItemStyle.BackColor = System.Drawing.Color.Yellow;
        gvBandA.Columns[1].ItemStyle.ForeColor = System.Drawing.Color.Fuchsia;
    }
于 2014-07-17T10:02:28.213 回答