DataTable编辑未正确显示

时间:2017-04-17 19:22:54

标签: c# datatable instance edit

我一直在寻找我的问题的答案很长一段时间,我还没有找到解决方案。 PS:我正在用德语进行测验游戏,所以你可能无法理解变量名等,但我认为这不应该是问题。

我在类SQL中有一个包含8列的DataTable,在最后一列中有一列为0.

namespace Quiz
{
    class SQL
    {
        public static DataTable GEO_Fragen()
        {
            using (DataTable GEO = new DataTable("Geografie"))
            {

                GEO.Columns.Add("ID", typeof(int));
                GEO.Columns.Add("Frage", typeof(string));
                GEO.Columns.Add("Antwort 1", typeof(string));
                GEO.Columns.Add("Antwort 2", typeof(string));
                GEO.Columns.Add("Antwort 3", typeof(string));
                GEO.Columns.Add("Antwort 4", typeof(string));
                GEO.Columns.Add("Richtige Antwort", typeof(int));
                GEO.Columns.Add("Gefragt", typeof(int));

                GEO.Rows.Add(0, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(1, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(2, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(3, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(4, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(5, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(6, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(7, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(8, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(9, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(10, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(11, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(12, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(13, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(14, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(15, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(16, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(17, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(18, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);
                GEO.Rows.Add(19, "Question", "Answer 1", "Answer 2", "Answer 3", "Answer 4", 4, 0);

                return GEO;
            }
        }
        //...more code

在另一堂课中,我选择了一个随机问题。如果这个问题在最后一列中有一个“1”(意思是之前已经使用过),那么它会生成另一个。

public void Zufallszahl()
{
    zufall = rnd.Next(0, SQL.GEO_Fragen().Rows.Count);
    if (SQL.GEO_Fragen().Rows[zufall]["Gefragt"].ToString() == "1")
    {
        Zufallszahl();
    }
    else
    {
        lbl_Frage.Text = SQL.GEO_Fragen().Rows[zufall]["Frage"].ToString(); //Filling in a random Question and Answers
        rad_Antwort1.Text = SQL.GEO_Fragen().Rows[zufall]["Antwort 1"].ToString();
        rad_Antwort2.Text = SQL.GEO_Fragen().Rows[zufall]["Antwort 2"].ToString();
        rad_Antwort3.Text = SQL.GEO_Fragen().Rows[zufall]["Antwort 3"].ToString();
        rad_Antwort4.Text = SQL.GEO_Fragen().Rows[zufall]["Antwort 4"].ToString();

        rad_Antwort1.Checked = false; // No radio button is ticked
        rad_Antwort2.Checked = false;
        rad_Antwort3.Checked = false;
        rad_Antwort4.Checked = false;
    }
}

现在到了真正的问题:当我改变我随机选择的行中最后一列的值时,它在调试模式下很快就会这样做。但就在那之后,当我让我显示这个单元格的值时,它再次显示“0”。

private void btn_Weiter_Click(object sender, EventArgs e)
{
    if (rad_Antwort1.Checked == true || rad_Antwort2.Checked == true || rad_Antwort3.Checked == true || rad_Antwort4.Checked == true)
    {
        Pruefen(); // Checking the chosen answer
        lbl_Score.Text = "Punkte: " + Punkte; // Current points
        string kontrolle = SQL.GEO_Fragen().Rows[zufall]["Antwort 1"].ToString();
        SQL.GEO_Fragen().Rows[zufall]["Gefragt"] = 1; //Changing value of the last column in row "zufall" to "1"
        MessageBox.Show(SQL.GEO_Fragen().Rows[zufall]["Gefragt"].ToString()); //Shows "0" instead of "1"

        if (rad_Antwort1.Text == kontrolle) // Next question
        {
            Zufallszahl();
            while (Counter <= 10)
            {
                Counter = Counter + 1;
                break;
            }

            if (Counter > 10)
            {
                SQL.GEO_Fragen().Reset();
                btn_Weiter.Hide(); // After 10 questions back to main menu
                lbl_Frage.Hide();
                rad_Antwort1.Hide();
                rad_Antwort2.Hide();
                rad_Antwort3.Hide();
                rad_Antwort4.Hide();
                lbl_Titel.Show();
                lbl_Themen.Show();
                rad_Geografie.Show();
                rad_Geschichte.Show();
                rad_Serien.Show();
                rad_Videospiele.Show();
                btn_Start.Show();
            }
            //... more code

我尝试过很多不同的东西,我已经尝试过AcceptChanges()BeginEdit()EndEdit()。出于调试目的,我还试图将DataTable和edit-command放入一个方法中。然后编辑工作完美。

1 个答案:

答案 0 :(得分:0)

每次调用GEO_Fragen()方法时,都会创建一个DataTable的新实例。因此,您正在更新一个实例并检查完全不同的实例,这就是为什么它似乎没有更新。

您可以通过向SQL类添加静态字段和属性来实现Singleton设计模式:

private static DataTable geoFragenInstance;

public static DataTable GeoFragenInstance
{
   get 
   {
      if (geoFragenInstance == null)
      {
          geoFragenInstance = GEO_Fragen();
      } 

      return geoFragenInstance;
   }
}

要确保只有SQL的一个实例,您还应该添加一个私有默认构造函数:

private SQL() {}

否则,默认情况下您将获得一个公开的。

我还会制作您的GEO_Fragen()方法private

然后将SQL.GEO_Fragen()的所有引用替换为SQL.GeoFragenInstance

请注意,这是Singleton最简单的实现,其中包括线程安全(可能适用于您的目的)。

进一步阅读:

Implementing Singleton in C#

Design Patterns - Singleton Pattern (4 Examples)