我每隔一定时间就会以编程方式在 datagridview 中添加一些行。我想在datagridview中查看某些github存储库的状态,例如是否在线,特定时间存储库中有多少提交等。
因此,当超时时,我使用 DataGridView.Rows.Clear() 清除行,然后再次添加包含更新信息的存储库。
我的代码在这里:
Dim ok As Integer = 0
DataGridView1.Rows.Clear()
For Each r As RepoURL In _impControllerBpi.GetReposApi
DataGridView1.Rows.Add(DateTimeOffset.Now, "https://github.com/" + r.GetUsr + "/" + r.GetNomRep, "-", "-", "-")
Next
Dim counter = 1
For Each r As RepoURL In _impControllerBpi.GetReposApi
DataGridView1.Rows.Item(counter - 1).Cells.Item(0).Value = DateTimeOffset.Now
DataGridView1.Rows.Item(counter - 1).Cells.Item(2).Value = "Conectando con GitHub"
ok = Await _impController.getRepoGitHub(r, counter)
If ok = 1 Then
DataGridView1.Rows.Item(counter - 1).Cells.Item(0).Value = DateTimeOffset.Now
DataGridView1.Rows.Item(counter - 1).Cells.Item(2).Value = "Conectado"
Else
DataGridView1.Rows.Item(counter - 1).Cells.Item(0).Value = DateTimeOffset.Now
DataGridView1.Rows.Item(counter - 1).Cells.Item(2).Value = "Repositorio no disponible en GitHub"
End If
counter = counter + 1
Next
Dim wreturnCode As Integer = System.Threading.ThreadPool.QueueUserWorkItem(New System.Threading.WaitCallback(AddressOf RepeatAction), cancellation.Token)
ok = 0
一切正常,无需Clear()
。但是当我清除行时,在某些迭代中,当我添加第一行时,系统会向我抛出一个
系统.NullReferenceException
一些帮助?Clear()
释放为行集合分配的内存?
当你确实想清除DataGridView控件的数据时,有多种串行方式可供选择。
1.使用for循环将行数据一一清除。
for (int i = 0; i < dataGridView1.RowCount; i++)
{
dataGridView1.Rows.Remove(dataGridView1.Rows[0]);
}
2.直接将DataSource属性设置为NULL。
DataGridView.DataSource=null;
如果要将DataSet绑定到该控件,最好先将DataSet设置为NULL。
DataSet DataSet1=new DataSet();
DataSet1.clear();
3.上述方式可能会删除指定DataSet中的所有Table,因此您最好单独删除其Table。
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("val", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add(1, "good");
ds.Tables.Add(dt);
dt.TableName = "test";
dataGridView1.DataSource = ds.Tables[0];
因此,您可以使用以下代码来清除指定的表。
ds.Tables[0].Clear();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)