简体   繁体   English

如何刷新c#中的表单?

[英]How do I refresh a form in c#?

I've built a form which can add/delete records to an SQL database. 我已经构建了一个可以向SQL数据库添加/删除记录的表单。 When I add a record, I want the form to reload and the listbox with all the records to now include the newly added record. 当我添加一条记录时,我想要重新加载表单,并且包含所有记录的列表框现在包括新添加的记录。

I googled this and saw that a new thread was recommended to refresh the form, but the instructions weren't clear enough for a newbie like me. 我用谷歌搜索了这个,看到一个新线程被推荐刷新表格,但说明书对于像我这样的新手来说还不够明确。

Any help would be appreciated. 任何帮助,将不胜感激。

Thanks in advance. 提前致谢。

Edit: this is a desktop app using c# and not asp. 编辑:这是一个使用c#而不是asp的桌面应用程序。

Some of the controls are populated by a wizard that I ran and for others I coded the datasource myself. 一些控件由我运行的向导填充,而其他控件则由我自己编写数据源。

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();
        }
    }
}

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

WinForms 的WinForms

There is a great article over at MSDN covering the following: Give Your .NET-based Application a Fast and Responsive UI with Multiple Threads . MSDN上有一篇很棒的文章,内容如下: 为基于.NET的应用程序提供一个快速响应的多线程UI

When you want to Re-draw your window you can call the method Invalidate . 如果要重新绘制窗口,可以调用方法Invalidate However when you Re-bind a control; 但是,当您重新绑定控件时; setting a new datasource, it will update the content in it. 设置一个新的数据源,它将更新其中的内容。

Here are some great videos to watch for WinForms: "How Do I Videos" from WindowsClient.net . 以下是一些关于WinForms的精彩视频: 来自WindowsClient.net的“我如何视频”

WPF WPF

Here is a matching article to the one in WinForms covering: Build More Responsive Apps With The Dispatcher . 这是WinForms中的一篇文章,其中包括: 使用Dispatcher构建更多响应式应用程序

Herer are some great videos to watch for WPF: "How Do I Videos" , aslo from WindowsClient.net. Herer是一些很棒的视频,可以观看WPF: “我如何视频” ,来自WindowsClient.net。

ASP.NET ASP.NET

If you want to create more responsive webbapps you might want to consider having a look at jQuery and Ajax. 如果您想创建更具响应性的webbapp,可能需要考虑查看jQuery和Ajax。 You can then request a new part of your web-site and replace the old one. 然后,您可以请求网站的新部分并替换旧网站。

However if you are just doing a postback and you want to add items to your listbox, you can just call DataBind on the ListBox and it should re-bind the items in the data-source. 但是,如果您只是在进行回发并且想要将项目添加到列表框中,则可以在ListBox上调用DataBind ,它应该重新绑定数据源中的项目。

The underlying question isn't really about Winforms repaints, rather it's how to freshen the datasource to which the Winform controls are bound, or from which the controls are being manually populated in unbound mode, after the database has been changed by your client application. 基本问题不是关于Winforms重绘,而是在客户端应用程序更改数据库之后,如何刷新Winform控件绑定到的数据源,或者在未绑定模式下手动填充控件。

Unless you create the data model, your datasource object has no way of knowing that the data have been changed when a DML statement is executed by a command somewhere. 除非您创建数据模型,否则当某个命令执行DML语句时,您的数据源对象无法知道数据已被更改。 It all has to be "tied together". 这一切都必须“捆绑在一起”。 ADO.NET uses a 'disconnected recordset' model. ADO.NET使用“断开连接的记录集”模型。 The ADO.NET datalayer objects will raise events related to data i/o and data errors, and you must must attach listeners/eventhandlers to them; ADO.NET数据层对象将引发与数据i / o和数据错误相关的事件,您必须将侦听器/事件处理程序附加到它们; these eventhandlers must, in turn, invoke your presentation-layer code. 反过来,这些事件处理程序必须调用您的表示层代码。

At present you're just scratching the surface with the command object. 目前你只是用命令对象抓住表面。 Best thing to do would be to read one of the books that show you how to wire up eventhandlers to the ADO.NET event model. 最好的办法是阅读其中一本书,向您展示如何将事件处理程序连接到ADO.NET事件模型。

EDIT: Here's a link to get you started: http://msdn.microsoft.com/en-us/library/w9y9a401.aspx 编辑:这是一个让你入门的链接: http//msdn.microsoft.com/en-us/library/w9y9a401.aspx

If it's ASP.NET you can refresh the form by redirecting it to itself by using the following code: 如果它是ASP.NET,您可以通过使用以下代码将表单重定向到自身来刷新表单:

Response.Redirect(Request.RawRul);

If it's a Windows Application, you need to rebind the listbox control by setting its DataSource property again. 如果它是Windows应用程序,则需要通过再次设置其DataSource属性来重新绑定列表框控件。

If you're already calling Invalidate on the ListBox, you might want to add a debug statement to the Paint method to make sure it's really repainting. 如果您已在ListBox上调用Invalidate ,则可能需要向Paint方法添加一个调试语句,以确保它真正重新绘制。 If it is, then you should look into where Paint gets the state needed to draw the items. 如果是,那么你应该看看Paint获取绘制项目所需的状态。 Do you query the database inside Paint ? 你在Paint查询数据库吗? (I hope not.) Or is the form keeping its state in memory somewhere? (我希望不是。)或者是形式在某个地方保持其状态? In that case, you should make sure that you're keeping the in-memory state consistent with the database. 在这种情况下,您应该确保保持内存状态与数据库一致。

Have you thought of the performance impact of re-loading all the rows whenever one row is added/deleted/updated? 你有没有想过重新加载所有行对性能的影响,每当一个行添加/删除/更新? IMHO, reloading the entire data will just annoy the users and also increase the bandwidth usage. 恕我直言,重新加载整个数据只会惹恼用户,也会增加带宽使用量。 Instead, create POCO objects for each line of data. 而是为每行数据创建POCO对象。 Whenever there is a (successful) CRUD operation (which you perform on the BackgroundWorker component), just add it to the DataSource and call DataBind . 只要存在(成功)CRUD操作(您在BackgroundWorker组件上执行),只需将其添加到DataSource并调用DataBind

For more information on how to use BackgroundWorker , see this . 有关如何使用BackgroundWorker详细信息,请参阅此内容

Write Select query in Form_Load and call after where ever you need to refresh your form like, 在Form_Load中写入选择查询,然后在需要刷新表单的地方调用,如:

form_load(Object sender,Event_args e)
  {

   select Query to display on Loading form
  }
add_click(Object sender,Event_args e)
 {
 form_load(sender,e);
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM