Reputation: 6912
My gridview is dynamically generated from a Datatable read from SQL, now I want to set a particular column, say, "salary" to have a red background color. I've searched SO for many solutions but still have no clues.
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;
Upvotes: 2
Views: 5070
Reputation: 670
You could create a converter class
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;
}
}
Then using a row template bind the back color to whatever item in your database would be passed to the converter.
Upvotes: 1
Reputation: 1
Follow this Code:
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;
}
Upvotes: 0
Reputation: 263
Try Below Code:
gvUserInfo.Columns[0].ItemStyle.BackColor = System.Drawing.Color.Red;
Put the same code in BindGrid Function after gvUserInfo.DataBind();
Enjoy..
Upvotes: 2