【问题标题】:How to set the background color of a column in gridview of WPF如何在WPF的gridview中设置列的背景颜色
【发布时间】:2013-07-25 16:13:24
【问题描述】:

我的 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;

【问题讨论】:

  • 您是否尝试过使用 DataGridTemplateColumn?

标签: wpf gridview datatable


【解决方案1】:

试试下面的代码:

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

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

享受..

【讨论】:

  • gvUserInfo.Columns[0] 没有名为 ItemStyle 的属性
  • 应该是 Grid 的属性,你遇到了什么错误。
【解决方案2】:

你可以创建一个转换器类

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;
    }
  }

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

【讨论】:

    【解决方案3】:

    遵循此准则:

    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;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-09-14
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 2014-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      相关资源
      最近更新 更多