What is DataGridView.Rows.Clear()?

后端 未结 1 896
忘了有多久
忘了有多久 2021-01-27 12:51

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,

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-27 13:19

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

    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.Set DataSource property to NULL directly.

     DataGridView.DataSource=null;
    

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

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

    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();
    

    0 讨论(0)
提交回复
热议问题