c# - 如何在 C# 中刷新表单?

标签 c# winforms visual-studio

我构建了一个可以向 SQL 数据库添加/删除记录的表单。添加记录时,我希望重新加载表单,并且包含所有记录的列表框现在包含新添加的记录。

我用谷歌搜索了一下,看到有人推荐了一个新线程来刷新表单,但是对于像我这样的新手来说,说明不够清楚。

如有任何帮助,我们将不胜感激。

提前致谢。

编辑:这是一个使用 c# 而不是 asp 的桌面应用程序。

一些控件由我运行的向导填充,而对于其他控件,我自己编写了数据源。

namespace LomWindows
{
    public partial class Form1 : Form
    {
        SqlConnection myConnection;

        public Form1()
        {
            InitializeComponent();
            myConnection = new SqlConnection(global::LomWindows.Properties.Settings.Default.esConnectionString);
            tConnStr.Text = global::LomWindows.Properties.Settings.Default.esConnectionString;

            try
            {
                myConnection.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sqlComm = "INSERT INTO ES_TOOL ....";

            try
            {
                myCommand.ExecuteNonQuery();
            }
            catch (Exception exce)
            {
                Console.WriteLine(exce.ToString());
            }

            try
            {
                myConnection.Close();
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.ToString());
            }

            InitializeComponent();
            MessageBox.Show("Tool Added");
            this.Invalidate();
        }
    }
}

最佳答案

您可以在添加/编辑/删除后再次重新绑定(bind)控件,这将使用更新后的数据重新加载控件。

关于c# - 如何在 C# 中刷新表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983957/

相关文章:

c# - 从 C# 运行 Bash 命令

c# - 在C# Winform中改变TabControl Header的背景

winforms - 在 WinForms 中使用 XAML

c# - 我可以使用 hwnd/NativeWindow 获得设置我的 WinForms 窗体所有者的行为吗?

c++ - LinkList 代码中的错误

visual-studio - 是否可以在Visual Studio 2012中隐藏轮廓页边距?

visual-studio - Visual Studio 2010 的代码段设计器/编辑器

c# - Appbar 按钮标签自动定位 UWP

c# - 如何自动增加包版本号?

c# - 为什么 HttpResponseMessage 不显示其内容?