0

我有一个添加了表单的控制台应用程序。

此表单中有一个数据网格视图,所有属性都保留为默认值。

在 Program.cs 我在 Main 方法中有这段代码:

CompanyActions objCompanyActions = new CompanyActions();

List<Company> analyzedData = new List<Company>();

List<Company> CompaniesFromExternalSource = objCompanyActions.GetExternalCompanyData(@"company.csv");

analyzedData =  objCompanyActions.Compare(CompaniesFromExternalSource);
AnalysisForm objAnalysisForm = new AnalysisForm();

objAnalysisForm.ShowAnalysisData(analyzedData);
Application.Run(objAnalysisForm);

在表格中,我有以下代码:

public void ShowAnalysisData(List<Company> analysisData)
{
     analysisGridView.DataSource = analysisData;

     UpdateGridStyle();

}

  private void UpdateGridStyle()
    {
        foreach (DataGridViewRow row in analysisGridView.Rows)
        {
            string RowType = row.Cells[0].Value.ToString();

            if (RowType == "Insert")
            {
                row.DefaultCellStyle.BackColor = Color.Green;
              //  row.DefaultCellStyle.ForeColor = Color.Black;
            }
            else if (RowType == "Update")
            {
                row.DefaultCellStyle.BackColor = Color.Yellow;
               // row.DefaultCellStyle.ForeColor = Color.Black;
            }
            else
            {
                row.DefaultCellStyle.BackColor = Color.Gray;
              //  row.DefaultCellStyle.ForeColor = Color.Black;
            }
        }

这行不通。网格仍然为每一行保留默认的白色背景?我在这里做错了什么?

问候。

4

1 回答 1

0

我认为您在错误的时间调用了更改背景颜色的方法。尝试将它放在 CellFormatting 中,如下所示:

private void analysisGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewRow row = analysisGridView.Rows[e.RowIndex];
    string RowType = row.Cells[0].Value.ToString();

    if (RowType == "Insert")
    {
        row.DefaultCellStyle.BackColor = Color.Green;
        //  row.DefaultCellStyle.ForeColor = Color.Black;
    }
    else if (RowType == "Update")
    {
        row.DefaultCellStyle.BackColor = Color.Yellow;
        // row.DefaultCellStyle.ForeColor = Color.Black;
    }
        else
    {
        row.DefaultCellStyle.BackColor = Color.Gray;
        //  row.DefaultCellStyle.ForeColor = Color.Black;
    }
}
于 2013-03-19T10:36:53.437 回答