Datable.Rows.Contains(value) 导致缺少主键异常

问题描述

我正在尝试查看 DataTable 的一行是否包含一个值,但它显示“System.Data.MissingPrimaryKeyException: 'Table 没有主键。'”我不知道为什么会有一个键需要在一行中找到一个值,并且需要一些关于如何正确浏览行以找到值的指导。 我用注释标记了引发异常的 if 语句。

 DataTable OutputDV = new DataTable();
            DataView ScrumTeams = ISBL.GetScrumTeams();


            //this dataview returns this: [CountDate,ScrumTeam,DefectCount]
            DataView alldefects = ISBL.Getdefects(dtstartdate,dtenddate);
           

            //Adding the columns for the datview
            DataColumn ScrumTeamName = new DataColumn("ScrumTeam",typeof(string));
            ScrumTeamName.ReadOnly = true;
            OutputDV.Columns.Add(ScrumTeamName);

            //first create columns of all dates
            foreach (DaTarow x in alldefects.ToTable().Rows)
            {
                //if there isnt a column by name of x then add one
                if (!OutputDV.Columns.Contains(x["CountDate"].ToString())) 
                {
                    OutputDV.Columns.Add(x["CountDate"].ToString());
                }
                //CODE BELOW CAUSES EXCEPTION: 
                //System.Data.MissingPrimaryKeyException: 'Table doesn't have a primary key.' 
                if (!OutputDV.Rows.Contains(x["ScrumTeam"].ToString()) || OutputDV.Rows.Count == 0)
                {
                    OutputDV.Rows.Add(x["ScrumTeam"].ToString());
                }
            }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...