C# Datable.Rows.Contains(值)导致缺少主键异常

C# Datable.Rows.Contains(值)导致缺少主键异常,c#,asp.net,datatable,dataview,C#,Asp.net,Datatable,Dataview,我试图查看DataTable的一行是否包含值,但它显示“System.Data.MissingPrimaryKeyException:“表没有主键”。“我不确定为什么需要一个键来查找行中的值,并且需要一些关于如何正确遍历行以查找值的指导。 我用注释标记了引发异常的if语句 DataTable OutputDV = new DataTable(); DataView ScrumTeams = ISBL.GetScrumTeams(); //th

我试图查看DataTable的一行是否包含值,但它显示“System.Data.MissingPrimaryKeyException:“表没有主键”。“我不确定为什么需要一个键来查找行中的值,并且需要一些关于如何正确遍历行以查找值的指导。 我用注释标记了引发异常的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());
                }
            }

根据文档,要检查值是否包含在行中,
Contains
需要主键属性-您已经添加了该列(您通过代码手动添加了该列,您知道它在那里),为什么要检查并尝试再次添加它?大部分内容都不需要,你想做什么?@zaggler我想添加一组不同日期的专栏标题,然后将不同scrum团队的名称添加到scrum团队列中。@carolynsavas预先创建您的数据表和所有列,然后将您需要的所有数据添加到该数据表中。@zaggler问题是我的allDefects行的设置如下:[Date],[Scrumteam],[Count],但在我的新表中,我需要它像:[Scrumteam],[Count on Date 1]、[Count on Date 2]、[Count on Date 3]、[Count on Date等等,所以我需要遍历整个allDefects来收集所有日期和计数,以便一个scrum团队创建一个新行