Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Cross thread datagridview issues? C#

$
0
0

Hi,

I have a windows form that has a datagridview on it and I'm running into issues where it either doesn't populate, or it stops updating or it locks the program.  Other times it works well.

The datagridview's datasource is on a different thread in another class.  I'm  sure that this is a threading issue but I do not have a clue how to use an invoke here.  There are two buttons on the form.

Windows form code:

 public partial class Form1 : Form
    {
        spotCollector _spotCollector = null;

        public Form1()
        {
            InitializeComponent();
            this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(dataGridView1_DataError);
        }

There is a button that I click on that makes a connection

        private void connectBTN_Click(object sender, EventArgs e)
        {
            spotCollector _collector = new spotCollector();
            _spotCollector = _collector;
            _spotCollector.connect(urltextBox1.Text);
        }

There is another button that starts the population of the datagridview.

       private void getSpots_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = _spotCollector.getSpotTable();

            for (int i = 0; i < dataGridView1.ColumnCount; i++) { dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } 
            dataGridView1.Refresh();
        }

Now the class that is use is as follows:

class spotCollector

{

        public spotCollector()
        {
            
        }

            

       public bool connect(string url)
        {
            if (url == null)
            {
                return false;
            }

            else
            {
                url = url + url_command;
            }

            dt = new DataTable();

            dt.Columns.Add("Spotter", typeof(string));
            dt.Columns.Add("Frequency", typeof(string));
            dt.Columns.Add("DX", typeof(string));
            dt.Columns.Add("Comment", typeof(string));
            dt.Columns.Add("DXCC", typeof(string));
            dt.Columns.Add("Time", typeof(string));

            bw.WorkerSupportsCancellation = true;
            bw.DoWork +=new DoWorkEventHandler(bw_DoWork);                            
            bw.RunWorkerAsync();

            return true;
        }

        public DataTable getSpotTable()
        {
            return dt;
        }

And the background worker thread basically clears the datatable and then repopulates it every 60 seconds

I'm sure that the problems I have been having are related to the cross threading.  But the debugger never catches it either.

Thanks for any help.

Tom


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>