C# 更改DataGridView行单元格颜色会导致NullReferenceException

C# 更改DataGridView行单元格颜色会导致NullReferenceException,c#,C#,我需要更改DataGridView行单元格颜色 这将生成“{”对象引用未设置为对象的实例。“}”,“System.NullReferenceException” Results=newlist(); foreach(类别中的Domain_Entites.TransactionView D3) { 字符串cusno=Convert.ToString(D3.CustomerNo.Trim(); 字符串cusname=D3.CustomerName.ToLower().Trim(); if(cusno

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

这将生成
“{”对象引用未设置为对象的实例。“}”,“System.NullReferenceException”

Results=newlist();
foreach(类别中的Domain_Entites.TransactionView D3)
{
字符串cusno=Convert.ToString(D3.CustomerNo.Trim();
字符串cusname=D3.CustomerName.ToLower().Trim();
if(cusno.Contains(memberno.Trim())==true&&cusname.Contains(customername.ToLower().Trim())==true)
{
如果(状态==D3.状态| |状态==0)
{
DataGridViewRow行=新建DataGridViewRow();
添加(新的DataGridViewTextBoxCell{Value=index});
添加(新的DataGridViewTextBoxCell{Value=D3.CustomerNo});
添加(新的DataGridViewTextBoxCell{Value=D3.Name6});
添加(新的DataGridViewTextBoxCell{Value=D3.Name1});
添加(新的DataGridViewTextBoxCell{Value=D3.Name2});
添加(新的DataGridViewTextBoxCell{Value=D3.Name3});
添加(新的DataGridViewTextBoxCell{Value=D3.Name4});
添加(新的DataGridViewTextBoxCell{Value=D3.Name5});
添加(新的DataGridViewTextBoxCell{Value=D3.DOBDate});
添加(新的DataGridViewTextBoxCell{Value=D3.DOBYear});
添加(新的DataGridViewTextBoxCell{Value=D3.DateCreated});
添加(新的DataGridViewTextBoxCell{Value=D3.StatusDescription});
添加(新的DataGridViewTextBoxCell{Value=D3.SanctionlistDate});
row.Tag=D3;
结果。添加(行);
如果(D3.Name6Matched==1)
dgvGridToProcess.Rows[i]。单元格[1]。Style.ForeColor=Color.Red;
如果(D3.Name1Matched==1)
dgvGridToProcess.Rows[i]。单元格[2]。Style.ForeColor=Color.Red;
如果(D3.Name2Matched==1)
dgvGridToProcess.Rows[i]。单元格[3]。Style.ForeColor=Color.Red;
如果(D3.name3匹配==1)
dgvGridToProcess.Rows[i]。单元格[4]。Style.ForeColor=Color.Red;
如果(D3.Name4Matched==1)
dgvGridToProcess.Rows[i]。单元格[5]。Style.ForeColor=Color.Red;
如果(D3.name5匹配==1)
dgvGridToProcess.Rows[i]。单元格[6]。Style.ForeColor=Color.Red;
如果(D3.DOBMatched==1)
dgvGridToProcess.Rows[i]。单元格[7]。Style.ForeColor=Color.Red;
如果(D3.0匹配==1)
dgvGridToProcess.Rows[i]。单元格[8]。Style.ForeColor=Color.Red;
i++;
}
}
索引++;
}
dgvDashboard1.Rows.AddRange(Results.ToArray());


发布完整堆栈跟踪rry我不明白你所说的“完整堆栈跟踪”是什么意思。你能解释一下什么语言、操作系统……你能调试一下并让我们知道你在哪一行得到这个错误吗?
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());