1

I create project (.NET 3.5) for send messages between pc (500 computers in company). How can I reload datagridview when in MySql is added new line? Who can help me with DBMS trigger? Or should I go for some other solution? Here is my code:

 private void Form1_Load(object sender, EventArgs e)
        {
            FillData();
        }

 public void FillData()
    {

        using (MySqlConnection myConnection = 
               new MySqlConnection("Server=10.7.18.35;Database=OitDB;Uid=martin;Pwd=;"))
        {
            myConnection.Open();

            using (MySqlDataAdapter sqlDa = new MySqlDataAdapter(
                   "select * from nrp", myConnection))
            {
                DataTable dt = new DataTable();
                sqlDa.Fill(dt);
                dataGridView1.DataSource = dt;
                this.dataGridView1.Refresh();
                dataGridView1.Columns[0].Width = 125;
                dataGridView1.Columns[1].Width = 500;
                dataGridView1.Columns[2].Width = 125;                  
                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
            }

        }
    }
Martin
  • 77
  • 2
  • 12
  • Maybe [this post](http://stackoverflow.com/questions/5668679/does-mysql-permit-callbacks-in-c-such-that-when-a-change-happens-i-can-be-notif) is of interest. Sounds cleaner than periodically asking the DBMS if anything was added.. Of course a little harder than always polling, __unless__, of course the new rows __come from your own application__..!? – TaW Dec 12 '15 at 12:17
  • yes new rows come from my own application – Martin Dec 12 '15 at 12:31
  • Ok, then all you need is to refresh the data binding. One simple way is to set the DataSource = `null` and then back to the table `dt`, of course making sure the table has the new date, maybe by calling fill once more.. Basically this amounts to calling FillData agian. - Of course if we are talking of several instances of your program this will not help. If that's the case, please clarify your question. Also include if they are running on the same machine or maybe on different machines!! – TaW Dec 12 '15 at 13:32
  • It will be running on 500 computers in company. So on different machines. Can help me set the `DataSource = null` and then back to the table `dt`? – Martin Dec 12 '15 at 13:46
  • Ok, this will then need either communication over the network or going for a DBMS trigger & user-defined function calling some helper app that the paplication can subscribe to. The alternative can be polling the DBMS for either the row count or maybe a lastRowAdded datetime field.. Try to define the delay that is acceptable util a refresh gets triggered! And do write all this into the question!! I can't help with code as I'm out of this sort of setup now, but other should get the full picture from the question without reading the comments!! – TaW Dec 12 '15 at 13:54
  • OK. acceptable delay until a refresh gets trigger is 10 sec – Martin Dec 12 '15 at 14:20

0 Answers0