System.Data.DataRowView 错误

System.Data.DataRowView error

所以,I.ve 实际上在谷歌上搜索了很多关于这个错误的信息,但是一些代码有一个我无法理解的解决方案,主要是因为我是 c# 的新手,所以我只是把问题本身。

我的教授称它为 "complex winform"。这基本上是来自与内部联接链接的 2 个不同 table 的数据。到目前为止一切顺利。

我使用 postgresql 顺便说一句。

我有 2 个主要 table。学生(带有 idstudent、registrationid、yearofstudy)和人员(带有 idperson、姓名、电话、电子邮件等)。 (idstudent = idperson)

我的数据库中大约有 20 个人和 7 个学生。学生也是人(duuh),ergo idstudent=idperson。

所以,我有一个组合框,我在其中放置了我所有学生的不同学习年份,它看起来像这样。

private void frmComplex1_Load(object sender, EventArgs e)
{

    OdbcConnection conexiune;
    conexiune = new OdbcConnection();
    conexiune.ConnectionString = "Driver={PostgreSQL ANSI};database=postgres;server=localhost;port=5432;uid=postgres;sslmode=disable;readonly=0;protocol=7.4;fakeoidindex=0;showoidcolumn=0;rowversioning=0;showsystemtables=0;fetch=100;unknownsizes=0;maxvarcharsize=255;maxlongvarcharsize=8190;debug=0;commlog=0;usedeclarefetch=0;textaslongvarchar=1;unknownsaslongvarchar=0;boolsaschar=1;parse=0;extrasystableprefixes=dd_;lfconversion=1;updatablecursors=1;trueisminus1=0;bi=0;byteaaslongvarbinary=0;useserversideprepare=1;lowercaseidentifier=0;gssauthusegss=0;xaopt=1;pwd=irimia96";
    conexiune.Open();

    OdbcCommand comanda;
    comanda = new OdbcCommand();
    comanda.CommandText = "SELECT DISTINCT anstudiu from studenti ORDER BY anstudiu asc ";
    comanda.Connection = conexiune;

    OdbcDataReader cititor;
   cititor = comanda.ExecuteReader();

    DataSet dsDate;
    dsDate = new DataSet();
    DataTable tblStudenti;
    tblStudenti = new DataTable("studenti");
    tblStudenti.Load(cititor);
    dsDate.Tables.Add(tblStudenti);

    this.cboComplex1.DataSource = dsDate.Tables["studenti"];
    this.cboComplex1.DisplayMember = "anstudiu";
    this.cboComplex1.ValueMember = "anstudiu";








    conexiune.Close();
}

所以我想做的是,每当我 select 一年 (1/2/3) 从那个组合框进入 return,在我的第一个 DataGrindView 关于学生的信息中来自学生 table 和个人 TABLE 的 1/2/3 年级。例如:yearofstudy 2 的学生。 IdPerson、姓名、电话、电子邮件、RegistrationId、学生 ID。 (我知道 Student Id 和 Person Id 会得到相同的值,但我不在乎,先让它起作用)

所以我输入了脚本,并得到了 datarawview 错误

private void cboComplex1_SelectedIndexChanged(object sender, EventArgs e)
{
    OdbcConnection conexiune;
    OdbcCommand comanda;
    DataSet dsDate;
    OdbcDataReader cititor;
    DataTable tblPersoane;


    conexiune = new OdbcConnection();
    conexiune.ConnectionString = " Driver={PostgreSQL ANSI};database=postgres;server=localhost;port=5432;uid=postgres;sslmode=disable;readonly=0;protocol=7.4;fakeoidindex=0;showoidcolumn=0;rowversioning=0;showsystemtables=0;fetch=100;unknownsizes=0;maxvarcharsize=255;maxlongvarcharsize=8190;debug=0;commlog=0;usedeclarefetch=0;textaslongvarchar=1;unknownsaslongvarchar=0;boolsaschar=1;parse=0;extrasystableprefixes=dd_;lfconversion=1;updatablecursors=1;trueisminus1=0;bi=0;byteaaslongvarbinary=0;useserversideprepare=1;lowercaseidentifier=0;gssauthusegss=0;xaopt=1;pwd=irimia96";
    conexiune.Open();

    comanda = new OdbcCommand();
    comanda.CommandText = "SELECT * from persoane INNER JOIN studenti on persoane.idpersoana = studenti.idstudent WHERE anstudiu =?";
    comanda.Connection = conexiune;


    comanda.Parameters.Clear();
    comanda.Parameters.AddWithValue("anstudiu", cboComplex1.SelectedValue.ToString());

    cititor = comanda.ExecuteReader();
    tblPersoane = new DataTable("persoane");
    tblPersoane.Load(cititor);
    dsDate = new DataSet();
    dsDate.Tables.Add(tblPersoane);

    dGComplex.DataSource = dsDate;
    dGComplex.DataMember = "persoane";
    dGComplex.Refresh();


}

久等了post,最后送你一个土豆

只需将 DisplayMember/ValueMember 的设置顺序与 DataSource 属性

的设置相反
this.cboComplex1.DisplayMember = "anstudiu";
this.cboComplex1.ValueMember = "anstudiu";
// Move this line after setting the Disply/ValueMember property
this.cboComplex1.DataSource = dsDate.Tables["studenti"];

这应确保用于 Display/ValueMember 的字符串与数据表的字段名称正确绑定。
我应该补充一点,如果您输入错误的字段名称之一(例如 "anstdiu" 将被接受),此模式不会捕获错误。
相反,如果您在 Display/ValueMember 之前设置 DataSource,尝试写入无效名称将导致运行时异常。