【发布时间】:2016-12-20 08:23:08
【问题描述】:
我有一个 Windows 表单,它需要永远将数据加载到我的 datagridview 中。
我不断收到这一行的错误:
dataGridView1.Rows.Add(row);
跨线程操作无效:控件“dataGridView1”从创建它的线程以外的线程访问。
这是我的代码:
public List<string> _checked = new List<string>();
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();
}
}
}
请告诉我哪里出错了
谢谢
【问题讨论】:
标签: c# multithreading