【问题标题】:how to truncate a table and update the datagridview that is link with the table如何截断表并更新与表链接的datagridview
【发布时间】:2018-10-16 13:01:39
【问题描述】:

我真的很需要帮助。我正在制作一个学生违规系统,我需要制作一个按钮,可以删除我在我的表和链接到该表的 datagridview 中的所有记录。所以当我点击按钮时,它应该要求确认,然后当用户点击是时。该表将被截断,datagridview 中的记录将刷新。我很难过,你能帮帮我吗?谢谢。

private void button2_Click_1(object sender, EventArgs e)
        {
            string query = "Truncate table [transaction]";
            using (SqlConnection xcon = new SqlConnection(@"Server=GLR\SQLEXPRESS;Database=SE_Project;Integrated Security=SSPI;"))
            {
                using (SqlCommand xcom = new SqlCommand(query, xcon))
                {
                    SqlDataAdapter xdapter = new SqlDataAdapter(xcom);
                    try
                    {
                        xcon.Open();
                        if (this.dataGridViewAdminTransac.DataSource != null)
                        {
                            this.dataGridViewAdminTransac.DataSource = null;
                            MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        }
                        else
                        {
                            this.dataGridViewAdminTransac.Rows.Clear();
                            MessageBox.Show("Are you sure?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        }

                        AdminView temp = new AdminView();
                        temp.Show();
                        this.Hide();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        xcon.Close();
                    }

                }
            }
        }

【问题讨论】:

    标签: c# datagridview refresh truncate


    【解决方案1】:

    不要;-) 让框架为您完成工作。使用绑定列表。您更新 BindingList。框架更新 DataGridView。

    在此处查看 Jürgen Steinblock 对机制的回答:Binding List<T> to DataGridView in WinForm

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 2010-12-02
      • 2017-02-21
      • 1970-01-01
      相关资源
      最近更新 更多