【问题标题】:C# Data of New Column won't show in Datagridview新列的 C# 数据不会显示在 Datagridview 中
【发布时间】:2021-03-19 08:58:27
【问题描述】:

所以我创建了一个名为“seatNumber”的新列,但它不会显示在 Datagridview 中。

但除了新列之外,其余数据正在显示并正在更新。

我这里有一个视频链接,以防万一。 https://youtu.be/adsT7h2uh48

这是预订按钮的代码:

private void reservationButton_Click(object sender, EventArgs e)
    {
        //Reservation Button.
        string nameString = nameTextbox.Text;
        string mobileString = mobileTextbox.Text;
        string timeString = timeComboBox.Text;
        string seatNumberString = textBox1.Text;
        bool allPopulatedBool = nameString != string.Empty && mobileString != string.Empty && timeString != string.Empty && seatNumberString != string.Empty;
        if (allPopulatedBool)
        {
            //if the Form are completed.
            SqlConnection con = new SqlConnection("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=LocalDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
            con.Open();
            string queryString = "INSERT INTO CustomersInfo VALUES(@CustomersName, @CustomersMobile, @CustomersTime, @seatNumber)";

            SqlParameter param1 = new SqlParameter();
            param1.ParameterName = "@CustomersName";
            param1.Value = nameString;

            SqlParameter param2 = new SqlParameter();
            param2.ParameterName = "@CustomersMobile";
            param2.Value = mobileString;

            SqlParameter param3 = new SqlParameter();
            param3.ParameterName = "@CustomersTime";
            param3.Value = timeString;

            SqlParameter param4 = new SqlParameter();
            param4.ParameterName = "@seatNumber";
            param4.Value = seatNumberString;

            SqlCommand command = new SqlCommand(queryString, con);
            command.Parameters.Add(param1);
            command.Parameters.Add(param2);
            command.Parameters.Add(param3);
            command.Parameters.Add(param4);

            command.ExecuteNonQuery();
            con.Close();
            MessageBox.Show("Reservation has been Successfully Booked!", "Schedule Added", MessageBoxButtons.OK, MessageBoxIcon.Information);
            nameTextbox.Text = string.Empty;
            mobileTextbox.Text = string.Empty;
            timeComboBox.Text = string.Empty;
            textBox1.Text = string.Empty;
        }
        if (!allPopulatedBool)
        {
            //if the Form are not completed.
            MessageBox.Show("Please fill out all fields.", "Incomplete Entry", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

【问题讨论】:

  • 您应该在第二种形式的“预订预订”按钮点击事件中发布代码。如果没有更多信息,就无法判断为什么座位号值不显示。正如 Gerry B 所说,我们必须假设座位号值已保存到数据库中。既然你说... “我创建了一个名为'seatNumber”的新列 ...我们可以看到网格中的列和DB,那么,不难假设代码中的部分第二个表单的Book Reservation 按钮单击事件可能尚未更新以包含新添加的座位号数据。
  • 另外,你评论了...... “当我按下数字时,它作为字符串进入文本框,然后将其分配为与其他数据相同的数据” ...? …如果座位号栏是新添加的,那么可以安全地假设在此之前没有“座位号”文本框。即使它在那里,也不会有任何代码从“座位号”文本框中获取值并将其保存到数据库中。所以,我看不出这怎么可能是“与其他数据相同”,因为它不存在,您一定是添加了代码,或者,我打赌您没有将代码更新为将座位号保存到数据库中。
  • 谢谢你,我还在网上搜索如何更新它,因为我不确定如何更新它,但我确定我会编辑它并发布代码:)
  • 好了,可以看到数据库中的数据了。您需要仔细查看...当您单击网格数据源时,有四 (4) 个不同的数据源...您确定您选择的是正确的吗?另外,在发布的图片中,除了座位号之外,所有列名都与表格列名匹配,这看起来很奇怪。您是否更改了该列标题文本?在网格中它的“座位号”,在数据库中它的“座位号”?最后,如果一切看起来都不错,但值仍然不显示,则创建一个新项目并将数据加载到网格中,无需任何额外代码。
  • 哦,谢谢,我查了一下,改成“seatNumber”,现在没问题,但是是这样的:youtu.be/8bg9yi8wyGs

标签: c# sql datagridview


【解决方案1】:

很遗憾,我无法发表评论,但必须将其发布为答案;您是否也更新了绑定/数据源?似乎该列确实出现在 DataGridView 中,您的问题是没有显示数据,这通常指向绑定/数据源 (SQL) 的输出中没有该列并且绑定正确。

【讨论】:

  • 好的,我会搜索它谢谢你:) 稍后我会告诉你
【解决方案2】:

您是否在接受预订时实际为座位号字段分配了数据?我看到您选择了 4 号座位,但这并不意味着它在数据中。

【讨论】:

  • 是的,当我按下数字时,它作为字符串进入文本框,然后将其分配为与其他数据相同的数据
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 2011-01-26
相关资源
最近更新 更多