我的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;
答案 0 :(得分:2)
尝试以下代码:
gvUserInfo.Columns[0].ItemStyle.BackColor = System.Drawing.Color.Red;
在gvUserInfo.DataBind();
享受..
答案 1 :(得分: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;
}
}
然后使用行模板将背景颜色绑定到数据库中将传递给转换器的任何项目。
答案 2 :(得分: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;
}