Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DGV - Show Live MYSQL DB table

I have a scenario where I have multiple users (around 50 at one time) connected to one table all make changes constantly to rows that already exist. What I need to do is somehow always show a live view of the database.

Having a cached copy of the table is not a valid choice because all users need to see all changes instantly.

When I first load the database I am using

mydtadp = new MySqlDataAdapter();
bindingSource1 = new BindingSource();
table = new DataTable();
MySqlConnection MyConn = new MySqlConnection(MyConnection);
MyConn.Open();

mydtadp.SelectCommand = new MySqlCommand("SELECT * FROM `Appointments`", MyConn);
cmbl = new MySqlCommandBuilder(mydtadp);


mydtadp.Fill(table);

bindingSource1.DataSource = table;
dataGridView1.DataSource = bindingSource1;

However, when another user makes a change this is not updating the local DataGridView to reflect the change. After searching the web I tried setting the VirtualMode property to true. Still no luck.

Next, I created a timer that would tick once a second that would call

dataGridView1.Refresh();

Still, when another user would update the database the changes wouldn't be reflected on the local DGV. I found another article that suggested trying

bindingSource1.ResetBinding(false); 

I put that on my timer on tick function, still not updating.

I found an article on MDSN that suggested that I try calling the mydtadp.Fill(table); function again in the timer function. Same thing.

The only thing that I could get to work was to save the index of the currently selected row then fully reload the dataset then reselect that index. However, this seems clunky and there has to be a better way to do it. This method seems like bad practice, and it does not work very well the DGV flashes whenever the timer ticks and when the user moves their cursor it takes a couple of seconds for the datagridview to catch up with the user. When the user makes changes to a row each keystroke takes a full-timer tick to update.

like image 475
Marcello B. Avatar asked Jun 02 '18 06:06

Marcello B.


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

I wouldn't recommend you fully update DGV each time since this will bring to performance issues.

You can add DateTime column to your MySQL table, and when the database table is updated, you just put current DateTime into it. In your WinForms app, you also add a variable for keeping last query DateTime.

Then when the timer ticks, you just select only those rows, which were updated since the DateTime variable.

Finally you individually update corresponding rows of the DGV.DataSource (I believe it is System.Data.DataView) with the fresh data from the SQL query.

like image 149
Tiber Septim Avatar answered Oct 15 '22 13:10

Tiber Septim