使用List(Of Entity)绑定到DataGridView Datasource属性会抛出空引用异常

时间:2013-09-01 00:41:03

标签: c# vb.net datagridview

我试着为它做了近4个小时的解决方案,但我找不到任何

public class FinalClassScoreModel
{
    private readonly FinalClassScore _finalClassScore;

    public FinalClassScoreModel(FinalClassScore finalClassScore)
    {
        this._finalClassScore = finalClassScore;
    }

    public int EnrollId
    {
        get
        {
            return this._finalClassScore.EnrollId;
        }
    }

    public string StudentNo
    {
        get
        {
            return this._finalClassScore.Enroll.Student.StudentNo;
        }
    }

    public string ReversedName
    {
        get
        {
            return this._finalClassScore.Enroll.Student.ReversedName;
        }
    }

    public decimal? Prelim
    {
        get
        {
            return this._finalClassScore.Prelim;
        }
    }

    public string PrelimE
    {
        get
        {
            return this._finalClassScore.PrelimE;
        }
    }

    public decimal? Midterm
    {
        get
        {
            return this._finalClassScore.Midterm;
        }
    }

    public string MidtermE
    {
        get
        {
            return this._finalClassScore.MidtermE;
        }
    }

    public decimal? Final
    {
        get
        {
            return this._finalClassScore.Final;
        }
    }

    public string FinalE
    {
        get
        {
            return this._finalClassScore.FinalE;
        }
    }

    public decimal? FinalGrade
    {
        get
        {
            return this._finalClassScore.FinalGrade;
        }
    }


    public bool IsDropped
    {
        get
        {
            return this._finalClassScore.Enroll.IsDropped;
        }
    }

    public bool IsIncomplete
    {
        get
        {
            return this._finalClassScore.Enroll.IsIncomplete;
        }
    }

    public bool IsGradeSubmitted
    {
        get
        {
            return this._finalClassScore.Enroll.IsGradeSubmitted;
        }
    }


Public Property FinalClassScoreModels() As IEnumerable(Of FinalClassScoreModel) Implements IAlternativeView.FinalClassScoreModels
        Get
            Return Me._finalClassScoreModels
        End Get
        Set(ByVal value As IEnumerable(Of FinalClassScoreModel))
            If DesignMode Then Exit Property
            Me._finalClassScoreModels = value
            Me.dgrAlternativeGrades.DataSource = Me._finalClassScoreModels
        End Set
    End Property

起初没关系但是当我删除FinalClassScoreModelList上的1个数据并重新运行Me.dgrAlternativeGrades.Datasource = Me._finalClassScoreModels时,它会抛出nullreference exception

public string StudentNo
    {
        get
        {
            return this._finalClassScore.Enroll.Student.StudentNo;
        }
    }

顺便说一下,如果我没有在datapropertyname上设置每列的datagridview,它就会没有错误,但我需要设置它,因为我不想自动生成列。

我的项目是vb.net和C#的组合

1 个答案:

答案 0 :(得分:0)

对于那些也会遇到这个问题的初学者,我在FinalClassScoreModel类上做了什么

public class FinalClassScoreModel
{
    private readonly FinalClassScore _finalClassScore;
    private readonly Enroll _enroll;

    public FinalClassScoreModel(FinalClassScore finalClassScore, Enroll enroll)
    {
        this._finalClassScore = finalClassScore;
        this._enroll = enroll;
    }

    public int EnrollId
    {
        get
        {
            return this._finalClassScore.EnrollId;
        }
    }

    public string StudentNo
    {
        get
        {
            return this._enroll.StudentNo;
        }
    }

通过这种方式它即可获得StudentNo的值,即使它是null或不是

感谢所有