How to update a form in C #?

I created a form that can add / remove records to an SQL database. When I add a record, I want the form to reload, and the list with all the records now includes the newly added record.

I googled this and saw that a new thread was recommended for updating the form, but the instructions were not clear enough for a beginner like me.

Any help would be appreciated.

Thanks in advance.

Edit: This is a desktop application using C #, not asp.

Some controls are populated with the wizard I ran, and for others I myself encoded the data source.

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(); } } } 
+4
source share
7 answers

After adding / editing / deleting, you can reinstall the control again, this will lead to a reset of the updated data controls.

+2
source

Winforms

There is a great article on MSDN: Give your .NET application a fast and responsive multi-threading interface .

If you want to redraw your window, you can call the Invalidate method. However, when you re-bind the control; By installing a new data source, it will update the contents in it.

Here are some great videos for WinForms: “Like me video” from WindowsClient.net .

WPF

Here is the related article in the one in WinForms: Create more responsive applications with Dispatcher .

Herer are great videos for watching WPF: "Like me video" , as well as from WindowsClient.net.

ASP.NET

If you want to create more responsive web browsers, you might want to take a look at jQuery and Ajax. You can then request a new part of your website and replace the old one.

However, if you just do the postback and want to add items to your list, you can just call the DataBind on the ListBox and bind the items in the data source.

+1
source

The main question is not what Winforms repaints are, but how to update the data source to which the Winform controls are bound, or from which the controls are manually populated in unrelated access mode, after the database has been modified by your client application .

If you are not creating a data model, your data source object cannot know that the data has changed when the DML command is executed by any command. All this should be "connected." ADO.NET uses the "disconnected record set" model. ADO.NET datalayer objects will create events related to data input / output errors and data, and you must connect listeners / event handlers to them; these event handlers should, in turn, invoke code at your presentation level.

Currently, you are simply scratching the surface with the command object. It would be best to read one of the books, which shows how to connect event handlers to the ADO.NET event model.

EDIT: here is the link you started with: http://msdn.microsoft.com/en-us/library/w9y9a401.aspx

+1
source

If ASP.NET you can update the form by redirecting it to yourself using the following code:

 Response.Redirect(Request.RawRul); 

If this is a Windows application, you need to reinstall the list control by setting its DataSource property again.

0
source

If you already call Invalidate on the ListBox, you can add a debug statement to the Paint method to make sure that it is actually redrawing. If so, then you should see where Paint gets the state necessary to draw the elements. Are you querying the database inside Paint ? (I hope not.) Or a form that retains its state in memory? In this case, you must make sure that you store the state in memory according to the database.

0
source

Have you thought about the effect of performance on reloading all rows every time a row is added / deleted / updated? IMHO, reloading all the information just annoys users, and also increases bandwidth usage. Instead, create POCO objects for each row of data. Whenever there is a (successful) CRUD operation (which you perform on the BackgroundWorker component), just add it to the DataSource and call the DataBind .

For details on how to use BackgroundWorker , see this .

0
source

Write a Select request to Form_Load and call after you sometime need to update the form, for example,

 form_load(Object sender,Event_args e) { select * from database; } add_click(Object sender,Event_args e) { form_load(sender,e); } 
0
source

Source: https://habr.com/ru/post/1339537/


All Articles