更改DataGridView行单元格颜色会产生NullReferenceException

时间:2018-05-16 14:01:59

标签: c#

我需要更改DataGridView行单元格颜色。

生成"{"Object reference not set to an instance of an object."}"," 'System.NullReferenceException'"

Results = new List<DataGridViewRow>();
foreach (Domain_Entites.TransactionView D3 in Category)
{
    string cusno = Convert.ToString(D3.CustomerNo).Trim();
    string cusname = D3.CustomerName.ToLower().Trim();

    if (cusno.Contains(memberno.Trim()) == true && cusname.Contains(customername.ToLower().Trim()) == true)
    {
        if (status == D3.Status || status == 0)
        {
            DataGridViewRow row = new DataGridViewRow();
            row.Cells.Add(new DataGridViewTextBoxCell { Value = index });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.CustomerNo });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.Name6 });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.Name1 });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.Name2 });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.Name3 });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.Name4 });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.Name5 });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.DOBDate });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.DOBYear });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.DateCreated });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.StatusDescription });
            row.Cells.Add(new DataGridViewTextBoxCell { Value = D3.SanctionlistDate });
            row.Tag = D3;

            Results.Add(row);

            if (D3.Name6Matched == 1)
                dgvGridToProcess.Rows[i].Cells[1].Style.ForeColor = Color.Red;
            if (D3.Name1Matched == 1)
                dgvGridToProcess.Rows[i].Cells[2].Style.ForeColor = Color.Red;
            if (D3.Name2Matched == 1)
                dgvGridToProcess.Rows[i].Cells[3].Style.ForeColor = Color.Red;
            if (D3.Name3Matched == 1)
                dgvGridToProcess.Rows[i].Cells[4].Style.ForeColor = Color.Red;
            if (D3.Name4Matched == 1)
                dgvGridToProcess.Rows[i].Cells[5].Style.ForeColor = Color.Red;
            if (D3.Name5Matched == 1)
                dgvGridToProcess.Rows[i].Cells[6].Style.ForeColor = Color.Red;
            if (D3.DOBMatched == 1)
                dgvGridToProcess.Rows[i].Cells[7].Style.ForeColor = Color.Red;
            if (D3.DOBYearMatched == 1)
                dgvGridToProcess.Rows[i].Cells[8].Style.ForeColor = Color.Red;
            i++;
        }
    }
    index++;
}
dgvDashboard1.Rows.AddRange(Results.ToArray());

upper

bottom

0 个答案:

没有答案