2012-01-08 32 views
6

如何刪除列標題以外的所有datagridview行?如何刪除表單加載上的所有DataGridView行?

我想:

dataGridView1.Rows.clear(); 

,但它不工作。

我試圖遍歷所有的行,並使用RemoveAt方法,但它不會刪除所有行:

private void Form5_Load(object sender, EventArgs e) 
{  
    dataGridView1.AutoGenerateColumns = true; 
    SqlConnection con = new SqlConnection(@"Data Source=.\myserver;Initial Catalog=test;Integrated Security=True"); 
    adapter = new SqlDataAdapter("SELECT id as [#], description as [Description], unit as [Unit], amount as [Amount], unitPrice as [Unit Price], total as [Total] FROM tbl_poMaterials", con); 
    adapter.SelectCommand.CommandType = CommandType.Text; 
    DataTable tb = new DataTable(); 
    adapter.Fill(tb); 
    SqlCommandBuilder builder = new SqlCommandBuilder(adapter); 
    dataGridView1.DataSource = tb; 

    dataGridView1.Columns[0].Width = 30; 
    dataGridView1.Columns[0].ReadOnly = true; 
    dataGridView1.Columns[1].Width = 660; 

    for (int i = 0; i < tb.Rows.Count; i++) 
    { 
     tb.Rows.RemoveAt(i); 
    } 
} 
+0

定義「但它不起作用」,會發生什麼? – 2012-01-08 07:51:19

+0

(DataGridView.Clear()](http://stackoverflow.com/questions/3744882/datagridview-clear) – 2016-06-13 17:02:13

回答

3

如果您的網格綁定到數據表或其他一些數據源,那麼你需要清除它,而不是網格,否則Rows.clear()方法是正確並且最好的方法。

+0

for(int i = 0; i user891757 2012-01-08 07:51:57

+0

你有你的datagridview綁定到一個數據表,所以清除數據表:tb.clear() – aleroot 2012-01-08 08:06:02

7

您需要清除DataSource或DataTable,而不是datagridview。

dataGridView.DataSource = null; 
dataGridView.Refresh(); 

dataTable.Clear(); 
dataGridView.Refresh(); 
+0

第二個解決方案爲我工作,感謝兄弟.. :-) – Praditha 2013-06-13 02:13:31

13

這爲我工作:

do 
{ 
    foreach (DataGridViewRow row in dataGridViewError.Rows) 
    { 
     try 
     { 
     dataGridViewError.Rows.Remove(row); 
     } 
     catch (Exception) { } 
    } 
} while (dataGridViewError.Rows.Count > 1); 
+0

奇怪,爲什麼爲和每個循環不工作和這個有用嗎?但好東西是有用的! – 2013-08-12 18:52:28

+0

感謝一年和幾個月後。我嘗試在我的DGV的Rows集合上使用'foreach',但是當它試圖刪除那個「空白」行時,我收到了一個異常。如果我添加了一個空值檢查,它只是刪除每隔一行?雖然這有效。 – sab669 2014-02-04 20:04:19

3

因爲我嘗試了幾種方法,但未能成功,同樣的問題。正如其中一個答案中所述:

for(int i = 0; i < myDataGridView.Rows.Count; i++) 
{ 
    myDataGridView.Rows.RemoveAt(i) 
} 

實際上會刪除該行,但下一行會移到上一行!所以,上面的方法刪除了一半的行數!因此,您需要重複該操作直至變爲零!

或者,嘗試從最後一行刪除到第一行。有用!

for(int i = myDataGridView.Rows.Count - 1; i >= 0; i--) 
{ 
    myDataGridView.Rows.RemoveAt(i); 
} 
4

我用

dataGridViewResult.Rows.Clear(); 

清除每行,而不刪除列。

0
int rowCount = dtg.Rows.Count; 
     for (int i = rowCount - 1; i >= 0; i--) 
     { 
      DataGridViewRow dr = dtg.Rows[i]; 
      dtg.Rows.Remove(dr); 
     } 
+0

請編輯你的答案,並解釋如何解決這個問題。 – Ben 2015-03-06 16:29:48

1

//這應該工作

int rowCount = dataGridView1.Rows.Count; 

for (int i = 0; i < rowCount; i++) 
{ 
    dataGridView1.Rows.RemoveAt(0); 
} 

您的解決方案沒有工作,因爲每次當for循環檢查的條件i < rowCount,該rowCount就已經下降了一個爲dataGridView1.Rows.RemoveAt(i)結果。所以只有一半的行會被刪除。

+0

請使用每個代碼行前面的4個空格來獲取代碼塊。 「你的解決方案」還不清楚。 – 2015-03-22 11:17:42

1

while(dataGridView1.Rows.Count >1) { dataGridView1.Rows.RemoveAt(0); }

+0

應該是while(dataGridView1.Rows.Count> 0) – Missy 2017-07-19 17:10:25

相關問題