【问题标题】:Issue with Insert function in Visual StudioVisual Studio 中的插入功能问题
【发布时间】:2014-01-21 15:39:18
【问题描述】:

这是我的代码:

private void button2_Click(object sender, EventArgs e)
{
    try
    {
        con = new SqlConnection();
        con.ConnectionString = constr;
        con.Open();

        string str1 = "Insert into SuperCars(Car ,mph,price) Values ('" + textBox2.Text + "'," + textBox3.Text + ",'" + textBox4.Text + "' )";
        cmd = new SqlCommand(str1, con);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Record Inserted successfully.");
        con.Close();
    }
    catch (Exception ex)
    {
        label5.Text = ex.ToString();
    }
}

我想在我的 GridView 中插入数据。当我执行此操作时,它会将数据插入到我的数据库中,但它没有显示在我的 GridView 中......我该如何编辑它?

【问题讨论】:

  • 上面的代码没有填充任何GridView。它只插入到数据库中。因此,您需要添加对实际填充 GridView 的代码的调用,最好将其添加到您的问题中。
  • 这里是链接尝试使用它来绑定网格[这里][1] [1]:csharptutorial.in/2013/09/…
  • 您需要小心 Little Bobby Tables 不会进入该文本框 (xkcd.com/327)

标签: c# winforms visual-studio gridview


【解决方案1】:

您必须通过再次设置 DataSource 来重新加载 GridView。

superCarsGridView.DataSource = null;
superCarsGridView.DataSource = GetSuperCars();

编辑:测试我的代码时,我在重新绑定 DataGridView 上的列表时遇到了问题。我通过将 DataSource 设置为 null 然后再次设置为真正的 DataSource 来解决这个问题。

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    相关资源
    最近更新 更多