[英]Changing Color in DataGridview rows C#
我現在有很多其他人問同樣的問題並得到答案。 但就我而言,我似乎無法使它正常工作。 我已經閱讀了幾篇建議在每行上使用DefaultCellStyle的文章。 我做了同樣的事情,但是我的顏色沒有改變。
這是我的代碼:
private void MyHistoryMainControl_Load(object sender, EventArgs e)
{
editedShipmentGrid.DataSource = handler.GetEditedShipments();
foreach (DataGridViewRow row in editedShipmentGrid.Rows)
{
if (string.IsNullOrWhiteSpace(row.Cells[54].FormattedValue.ToString()))
{
row.DefaultCellStyle.ForeColor = Color.Blue;
}
if (string.IsNullOrWhiteSpace(row.Cells[55].FormattedValue.ToString()))
{
row.DefaultCellStyle.ForeColor = Color.Red;
}
}
}
我正在使用的UserControl的Load方法中運行此代碼。 數據在DataGridView中的加載很好,但是顏色不變。 我還檢查了我的病情的布爾值,它應該改變顏色。 我在這里做錯了什么?
我找到了解決方案。 我必須在InitializeComponent()之后綁定數據。 方法。 在我的加載方法中,我根據單元格值更改顏色。
這是解決方案:
public MyHistoryMainControl()
{
InitializeComponent();
editedShipmentGrid.DataSource = handler.GetEditedShipments();
}
private void MyHistoryMainControl_Load(object sender, EventArgs e)
{
foreach (DataGridViewRow row in editedShipmentGrid.Rows)
{
if (string.IsNullOrWhiteSpace(row.Cells[54].FormattedValue.ToString()))
{
row.DefaultCellStyle.ForeColor = Color.Blue;
}
if (string.IsNullOrWhiteSpace(row.Cells[55].FormattedValue.ToString()))
{
row.DefaultCellStyle.ForeColor = Color.Red;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.