Adrian Manuel Cleofe
Adrian Manuel Cleofe

Reputation: 45

Real Time Update of DataGridVIew using Multithreading

I have database with a product_list table

+----+-------------+-------+-------+
| id | description | price | Stock |
+----+-------------+-------+-------+
| 1  | Item 1      | 1.50  |   5   |
+----+-------------+-------+-------+

Then I have a two windows form application. Each application is on separate project.

  1. First application is to insert a data to the table which has a query of INSERT INTO product_list (description, price, stock) VALUES ('Item 2', 2.00, 10).
  2. Second application is to view the table in real time using datagridview. i have a object for my database and have a function name GetTable() that return a DataTable.

Here's the code for the database object that return a DataTable.

     public DataTable GetTable()
     {
        DataTable table = new DataTable();

        try
        {
            MySqlDataAdapter daTable = new MySqlDataAdapter(this.query, this.conn);
            daTable.Fill(table);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        return table;
     }

Here the code for the multithreading

    DataTable inventoryTable;
    inventoryDB inventoryDB;

    Thread updateDataGridView;

    public Form1()
    {
        InitializeComponent();
        inventoryDB = new inventoryDB();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        inventoryDB.SetQuery("SELECT id AS ID, description as Description, price as Price, stock as Stock FROM `product_list");
        inventoryTable = inventoryDB.GetTable();

        this.dataGridView1.DataSource = inventoryTable;

        this.updateDataGridView = new Thread(new ThreadStart(this.RealTimeData));
        this.updateDataGridView.Start();
    }

    private void RealTimeData() {
        while (true) {
            testTable = inventoryDB.GetTable();

            this.dataGridView1.DataSource = testTable;
            this.dataGridView1.Update();
            this.dataGridView1.Refresh();
        }
    }

when i run visual studio give a InvalidOperationException.

Upvotes: 0

Views: 2852

Answers (1)

Vandita
Vandita

Reputation: 748

You can use InvokeRequired property of a control. Try this code.

DataTable inventoryTable;
inventoryDB inventoryDB;

Thread updateDataGridView;

public Form1()
{
    InitializeComponent();
    inventoryDB = new inventoryDB();
}

private void Form1_Load(object sender, EventArgs e)
{
    inventoryDB.SetQuery("SELECT id AS ID, description as Description, price as Price, stock as Stock FROM `product_list");
    inventoryTable = inventoryDB.GetTable();

    this.dataGridView1.DataSource = inventoryTable;

    this.updateDataGridView = new Thread(new ThreadStart(this.RealTimeData));
    this.updateDataGridView.Start();
}

private void RealTimeData() {
    testTable = inventoryDB.GetTable();
    this.SetDataSourceInGridView(testTable);
}

// This delegate enables asynchronous calls for setting  
// the datasource property on a GridView control.  
delegate void GridViewArgReturningVoidDelegate(DataTable testTable); 

private void SetDataSourceInGridView(DataTable testTable)
{
    // InvokeRequired required compares the thread ID of the  
    // calling thread to the thread ID of the creating thread.  
    // If these threads are different, it returns true.  
    if (this.dataGridView1.InvokeRequired)  
    {     
        GridViewArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(SetDataSourceInGridView);  
        this.Invoke(d, new object[] { testTable });  
    }  
    else  
    {  
        this.dataGridView1.DataSource = testTable;
        this.dataGridView1.Update();
        this.dataGridView1.Refresh();  
    }  
}

Upvotes: 1

Related Questions