Sunday 1 July 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...