Sunday, July 1, 2018

c# - Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on

I have a Windows form which is taking forever to load data into my datagridview.



I keep getting the error in this line:



    dataGridView1.Rows.Add(row);



Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on.



Here is my code:



    public List _checked = new List();

private void getBarcodProperty()
{

string query = "select Name from RfidUnic where Barcode = @barcode";

while (true)
{
while (_checked.Count > 0)
{
SQLiteCommand cmd = new SQLiteCommand(query, sqliteConnection);
cmd.Parameters.AddWithValue("@barcode", _checked[0]);
sqliteConnection.Open();


SQLiteDataReader da = cmd.ExecuteReader();

if (da.Read())
{
if (!da.IsDBNull(0))
{
string[] row = new string[] { da[0].ToString(), "1", _checked[0] };
dataGridView1.Rows.Add(row);
_checked.RemoveAt(0);
}

}
else
{
string[] row = new string[] { "empty", "1", _checked[0] };
dataGridView1.Rows.Add(row);
_checked.RemoveAt(0);
}

sqliteConnection.Close();
}

}
}


Please show me where I am going wrong



Thanks

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...