C# 如何在表单加载时删除所有 DataGridView 行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8776091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove all DataGridView rows on form load?
提问by user891757
How can I remove all the datagridview rows except the column headers?
如何删除除列标题之外的所有 datagridview 行?
I tried:
我试过:
dataGridView1.Rows.clear();
but it does not work.
但它不起作用。
I tried to loop over the rows and use the RemoveAtmethod, but it does not remove all rows:
我尝试遍历行并使用该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);
}
}
回答by aleroot
If your grid is bound to a DataTable or some other DataSource then you need to clear it, not the grid, otherwise the Rows.clear() method is the right and best way to do it.
如果您的网格绑定到 DataTable 或其他一些数据源,那么您需要清除它,而不是网格,否则 Rows.clear() 方法是正确和最好的方法。
回答by Ari
You need to clear the DataSource or DataTable and not the datagridview.
您需要清除 DataSource 或 DataTable 而不是 datagridview。
dataGridView.DataSource = null;
dataGridView.Refresh();
or
或者
dataTable.Clear();
dataGridView.Refresh();
回答by whoooooz
This worked for me:
这对我有用:
do
{
foreach (DataGridViewRow row in dataGridViewError.Rows)
{
try
{
dataGridViewError.Rows.Remove(row);
}
catch (Exception) { }
}
} while (dataGridViewError.Rows.Count > 1);
回答by KbManu
For the same issue I tried several ways but could not succeed. As said in one of the answers:
对于同样的问题,我尝试了几种方法,但都没有成功。正如其中一个答案中所说:
for(int i = 0; i < myDataGridView.Rows.Count; i++)
{
myDataGridView.Rows.RemoveAt(i)
}
will actually delete the row but the next row gets shifted to previous row ! So, the above approach deletes half of the number of rows ! Hence, you need to repeat the action till it becomes zero!
实际上会删除该行,但下一行会移到上一行!因此,上述方法删除了一半的行数!因此,您需要重复该动作,直到它变为零!
Alternatively, tried deleting from the last row to the first. It works!
或者,尝试从最后一行删除到第一行。有用!
for(int i = myDataGridView.Rows.Count - 1; i >= 0; i--)
{
myDataGridView.Rows.RemoveAt(i);
}
回答by StefanoM5
I use
我用
dataGridViewResult.Rows.Clear();
to clear every rows without delete columns.
清除每一行而不删除列。
回答by Novin.Kh
int rowCount = dtg.Rows.Count;
for (int i = rowCount - 1; i >= 0; i--)
{
DataGridViewRow dr = dtg.Rows[i];
dtg.Rows.Remove(dr);
}
回答by ramesh kumar
//this should work
//这应该工作
int rowCount = dataGridView1.Rows.Count;
for (int i = 0; i < rowCount; i++)
{
dataGridView1.Rows.RemoveAt(0);
}
Your solution didnt work because , everytime when the for loop checks for the condition i < rowCount, the rowCountwould have already decreased by one as a result of dataGridView1.Rows.RemoveAt(i). So only half of the rows would have been deleted.
您的解决方案不起作用,因为每次 for 循环检查条件时i < rowCount,rowCount都会由于dataGridView1.Rows.RemoveAt(i). 所以只有一半的行会被删除。
回答by Devanand Kamble
while(dataGridView1.Rows.Count >1)
{
dataGridView1.Rows.RemoveAt(0);
}
while(dataGridView1.Rows.Count >1)
{
dataGridView1.Rows.RemoveAt(0);
}
回答by Nishat Bhagat
Make row and column count zero and refresh data set.
使行和列计数为零并刷新数据集。
dataGrid.Rows.Count = 0;
dataGrid.Refresh();
dataGrid.Refresh();

