【问题标题】:Radio Button Binding Problem单选按钮绑定问题
【发布时间】:2011-10-17 17:36:07
【问题描述】:

我在将数据库值绑定到表单上的单选按钮时遇到问题。我有一个测验,在我的数据库中,我有每个可能答案的列,以及正确答案的列。

但是,在我的表单上出现的每个问题上,正确答案始终是第一个单选按钮,即使“正确答案”列中的值并不总是与第一个单选按钮中的值相同。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Quiz_Test
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    String chosenAnswer, correctAnswer;
    DataTable table;
    int questionNumber = 0;
    int correctAnswers = 0;

    private void Form1_Load(object sender, EventArgs e)
    {
        string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";

        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();

        conGet.Open();
        cmdGet.CommandType = CommandType.Text;
        cmdGet.Connection = conGet;

        cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()";

        OleDbDataReader reader = cmdGet.ExecuteReader();
        table = new DataTable();
        table.Load(reader);
        int recordCount = table.Rows.Count;

        foreach (DataRow row in table.Rows)
        {

            labelQuestion.Text = table.Rows[questionNumber]["Question"].ToString();
            radioButton1.Text = table.Rows[questionNumber]["Answer 1"].ToString();
            radioButton2.Text = table.Rows[questionNumber]["Answer 2"].ToString();
            radioButton3.Text = table.Rows[questionNumber]["Answer 3"].ToString();
            radioButton4.Text = table.Rows[questionNumber]["Answer 4"].ToString();
            correctAnswer = table.Rows[questionNumber]["Correct Answer"].ToString();
            recordCount++;
        } 
    conGet.Close();
    }

    private void btnGoToNextOne_Click(object sender, EventArgs e)
    {
        String cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";

        OleDbConnection conGet = new OleDbConnection(cnString);
        OleDbCommand cmdGet = new OleDbCommand();
        int recordCount = table.Rows.Count;

        {
            conGet.Open();

            cmdGet.CommandType = CommandType.Text;
            cmdGet.Connection = conGet;

            cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()"; // select all columns in all rows

            OleDbDataReader reader = cmdGet.ExecuteReader();
            reader.Read();
        if (questionNumber < recordCount)
        {

           if (radioButton1.Checked)
           {
              chosenAnswer = reader["Answer 1"].ToString();
           }
           else if (radioButton2.Checked)
           {
              chosenAnswer = reader["Answer 2"].ToString();
           }
           else if (radioButton3.Checked)
           {
              chosenAnswer = reader["Answer 3"].ToString();
           }
           else if (radioButton4.Checked)
           {
              chosenAnswer = reader["Answer 4"].ToString();
           }

           if (questionNumber < recordCount)
           {

               if (chosenAnswer == reader["Correct Answer"].ToString())
               {
                   labelQuestion.Text = table.Rows[questionNumber]["Question"].ToString();
                   radioButton1.Text = table.Rows[questionNumber]["Answer 1"].ToString();
                   radioButton2.Text = table.Rows[questionNumber]["Answer 2"].ToString();
                   radioButton3.Text = table.Rows[questionNumber]["Answer 3"].ToString();
                   radioButton4.Text = table.Rows[questionNumber]["Answer 4"].ToString();
                   correctAnswer = table.Rows[questionNumber]["Correct Answer"].ToString();
                   questionNumber++;
                   correctAnswers++;
               }
               else
               {
                   MessageBox.Show("This is incorrect");
               }
               }
               recordCount++;
           }
           else
           {
               MessageBox.Show("You have finished the test. You have " + questionNumber + " answers correct");
           }
           }
      }
  }  
}

【问题讨论】:

  • 我在您的代码中看不到您为单选按钮设置绑定或选中值的位置。您正在检查它是否为真,但我看不到您在哪里设置它。
  • 您好,这是我的全部代码,您能详细说明一下吗?

标签: c# winforms


【解决方案1】:

您似乎一直在增加 recordCount 变量而不是 questionNumber 变量。

您甚至不需要 recordCount 变量,只需使用 table.Rows.Count 属性即可。

我认为这应该基本上解决你的问题,虽然你有一些未使用的 foreach 循环等。

【讨论】:

  • 好的,我已经改了,虽然还没有解决问题
猜你喜欢
  • 2015-03-31
  • 2014-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多