什么是DataGridView.Rows.Clear()? [英] What is DataGridView.Rows.Clear()?

查看:293
本文介绍了什么是DataGridView.Rows.Clear()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每隔一定时间在datagridview中以程序方式添加一些行。我想在datagridview中查看某些github知识库的状态,例如是否在线,某些时间的回馈中有多少提交。

I'm adding programatically some rows in a datagridview every certain time. I want to view in the datagridview the status of certain github repositories, such as if are online, how much commits are in the repos at certain time, etc.

所以,当有超时时,我用DataGridView.Rows.Clear()清除行,然后我再次添加更新的信息的存储库。

So, when there is timeout, i clear the rows with DataGridView.Rows.Clear() and then i add again the repositories with the updated info.

我的代码在这里:

 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()。但是当我清除行时,在一些迭代中,当我添加第一行时,系统会抛出一个

All works fine without Clear(). But when i clear the rows, in some iteration, when i add the first row, the system throws me a


System.NullReferenceException


System.NullReferenceException

有些帮助? Clear()释放为集合行分配的内存

Some help? Clear() frees the memory allocated for the collection of rows?

推荐答案

当您真的要清除DataGridView Control的数据时,有一些选择的方法。

There are serial ways for choice when you really want to clear the data of DataGridView Control.

1.使用循环来逐行清除行数据。对于(int i = 0; i< dataGridView1.RowCount; i ++)
{
dataGridView1.Rows $ p

1.Use for loop to clear the row data one by one.

 for (int i = 0; i < dataGridView1.RowCount; i++)
        {
            dataGridView1.Rows.Remove(dataGridView1.Rows[0]);

        } 

2.将DataSource属性直接设置为NULL。 >

2.Set DataSource property to NULL directly.

 DataGridView.DataSource=null;

如果要将DataSet绑定到此控件,最好首先将DataSet设置为NULL。 / p>

If you want to bind DataSet to this control, you’d better setting DataSet to NULL first.

DataSet DataSet1=new DataSet();
DataSet1.clear();

3.一方面可以删除指定DataSet中的所有表,所以最好删除它表单分开。

3.Above way may remove all of Tables inside in specified DataSet,so you'd better removing its Table separately.

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];

因此,您可以使用以下代码清除指定的表。

Hence, you can use following code to clear specified table.

ds.Tables[0].Clear();

这篇关于什么是DataGridView.Rows.Clear()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆