DataTable Foreach - 获取 Table 数据但只返回一个结果

DataTable Foreach - Getting to Table Data but only returning one result

我有一个数据表(来自本地数据库),我正在尝试将答案 table 中的 "QuestionID" 与当前问题的 ID 进行比较,以便它显示RadioButtonList (ASP.Net).

中与该问题相关的四个答案

它的工作达到了这样一个程度,即它获得了第一个匹配的结果并将单选按钮列表放入其中,但它没有获得其他三个匹配的结果。为什么?看这段代码看得我头皮发麻

DataView dvQuestion = (DataView) sqldataQuestions.Select(new DataSourceSelectArguments());
lblQuestion.Text = string.Format(dvQuestion[questionCount]["QuestionText"].ToString());

DataView dvAnswers = (DataView) sqldataAnswers.Select(new DataSourceSelectArguments());
DataTable tblAnswers = dvAnswers.ToTable();
string value;

foreach (DataRow dr in tblAnswers.Rows)
{
      int questionID = Convert.ToInt32(dr["QuestionID"]);
      if (questionID == PgeNum)
      {
          string ansTxt = dr["AnswerText"].ToString();
          rblAnswers.Items.Add(ansTxt);
      }
}

尝试this.This 将过滤QuestionId 等于pgeNum 的行。比你可以在行上循环以获得你想要的结果。

DataRow[] rows =  tblAnswers.Select("QuestionID = " + PgeNum);
 if (rows.Length != 0)
    {
      foreach (DataRow dr in rows)
      {      
        string ansTxt = dr["AnswerText"].ToString();
        rblAnswers.Items.Add(ansTxt);
       }

     }