我想填充一个DataGridView基于组合框中选定的项目,我有这部分工作。如何清除数据网格视图
但是,我需要能够在添加新项目中的新数据之前清除网格,而不是仅仅添加到结尾。
如何在添加项目之前清除DataGridView?
我想填充一个DataGridView基于组合框中选定的项目,我有这部分工作。如何清除数据网格视图
但是,我需要能够在添加新项目中的新数据之前清除网格,而不是仅仅添加到结尾。
如何在添加项目之前清除DataGridView?
首先,空数据源:
this.dataGridView.DataSource = null;
然后清除行:
this.dataGridView.Rows.Clear();
然后将数据源设置为新名单:
this.dataGridView.DataSource = this.GetNewValues();
如果我不清空数据源会发生什么情况? – Patrick
DataGrid.DataSource = null;
DataGrid.DataBind();
简单简单。工作 –
在我的情况下,出于某种原因,Clear()不适用于我。我用这个代码,它完美的工作。 –
可以以这种方式清除DataGridView
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
如果是databound
那就试试这个
dataGridView1.Rows.Clear() // If dgv is bound to datatable
dataGridView1.DataBind();
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
您可以指定数据源作为数据网格的空,然后重新绑定。
dg.DataSource = null;
dg.DataBind();
这是DGV不是数据网格。容易的错误 - 我知道我做了足够多的时间。 – bendecko
刷新的DataGridView并刷新数据表
dataGridView1.Refresh();
datatable.Clear();
datatable.Clear();
dataGridView1.DataSource = datatable;
即使答案是正确的,我总是会加入一点解释,因为用户可能不会理解它,只是看你的代码。 – nKn
解决办法是:
while (dataGridView1.RowCount > 1)
{
dataGridView1.Rows.RemoveAt(0);
}
循环解决方案不合适... –
如果你想清除所有的头和数据,例如如果你在两个完全不同的数据库之间切换不同的领域,因此不同的列和列标题,我发现以下工作。否则,当你切换你有两个数据库显示在网格中的列/字段。
dataTable.Dispose();//get rid of existing datatable
dataTable = new DataTable();//create new datatable
datagrid.DataSource = dataTable;//clears out the datagrid with empty datatable
//datagrid.Refresh(); This does not seem to be neccesary
dataadapter.Fill(dataTable); //assumming you set the adapter with new data
datagrid.DataSource = dataTable;
如果它绑定到数据源 -
dataGridView.DataSource=null;
dataGridView.Rows.Clear();
为我工作。
我知道这是迟了约2年半,但如果datagridview未绑定到数据源呢?如何在刷新数据网格之前清除它? –
private void ClearGrid()
{
if(this.InvokeRequired) this.Invoke(new Action(this.ClearGrid));
this.dataGridView.DataSource = null;
this.dataGridView.Rows.Clear();
this.dataGridView.Refresh();
}
如果你想清除不仅数据,而且还组合框,复选框,尝试
dataGridView.Columns.Clear();
解决的办法是:
dataGridView1.Rows.RemoveAt(0);
清除电网并保留列。
它只会删除1行。清除网格使用方法'dataGridView1.Rows.Clear();' – ClearLogic
你可以采取下一条指令,并做缺乏性能的工作。如果你想看到这个效果,把你需要清除DataGridView的下一条指令(技术上相似)放到try {} catch(...){} finally块中,然后等待发生了什么。
while (dataGridView1.Rows.Count > 1)
{
dataGridView1.Rows.RemoveAt(0);
}
foreach (object _Cols in dataGridView1.Columns)
{
dataGridView1.Columns.RemoveAt(0);
}
您改善这一任务,但它不够,还有是因为仍然在DataGridView对象中柱的侧向承载力重置一个DataGridView问题。最后,我建议,我在家庭实践中实现的最佳方式是将此GridView作为具有行,列的文件处理:基于行和列之间匹配的记录集合。如果你可以改进,那么你可以选择a)或b):foreach or while。
//(a): With foreach
foreach (object _Cols in dataGridView1.Columns)
{
dataGridView1.Columns.RemoveAt(0);
}
foreach(object _row in dataGridView1.Rows){
dataGridView1.Rows.RemoveAt(0);
}
//(b): With foreach
while (dataGridView1.Rows.Count > 1)
{
dataGridView1.Rows.RemoveAt(0);
}
while (dataGridView1.Columns.Count > 0)
{
dataGridView1.Columns.RemoveAt(0);
}
那么,作为一个recomendation从来没有在你的生活先删除列的顺序是的cols后的行之前,因为逻辑上首先创建地方,然后rows.It就不能是一个点球列正确的分析。
foreach (object _Cols in dataGridView1.Columns)
{
dataGridView1.Columns.RemoveAt(0);
}
foreach (object _row in dataGridView1.Rows)
{
dataGridView1.Rows.RemoveAt(0);
}
while (dataGridView1.Rows.Count > 1)
{
dataGridView1.Rows.RemoveAt(0);
}
while (dataGridView1.Columns.Count > 0)
{
dataGridView1.Columns.RemoveAt(0);
}
然后,把它放在一个函数或方法中。
private void ClearDataGridViewLoopWhile()
{
while (dataGridView1.Rows.Count > 1)
{
dataGridView1.Rows.RemoveAt(0);
}
while (dataGridView1.Columns.Count > 0)
{
dataGridView1.Columns.RemoveAt(0);
}
}
private void ClearDataGridViewForEach()
{
foreach (object _Cols in dataGridView1.Columns)
{
dataGridView1.Columns.RemoveAt(0);
}
foreach (object _row in dataGridView1.Rows)
{
dataGridView1.Rows.RemoveAt(0);
}
}
最后,调用你的新函数ClearDataGridViewLoopWhile();或ClearDataGridViewForEach();您需要使用它的地方,但是当您正在进行查询并更改将在grieView中使用不同头名称加载的整体表时,会推荐使用它。但是,如果你想在这里保存标题,有一个解决方案。
这是工作给我
'int numRows = dgbDatos.Rows.Count;
for (int i = 0; i < numRows; i++)
{
try
{
int max = dgbDatos.Rows.Count - 1;
dgbDatos.Rows.Remove(dgbDatos.Rows[max]);
btnAgregar.Enabled = true;
}
catch (Exception exe)
{
MessageBox.Show("No se puede eliminar " + exe, "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}`
为什么一次只能删除一个,何时可以使用Clear()作为Trevor的建议?此外为什么每次循环运行时启用一个按钮?按钮启用应该已经从示例中删除,并且应该首先移到循环之外。 – nivs1978
YourGrid.Items.Clear();
YourGrid.Items.Refresh();
对于具有一个DataGrid您必须有它格式化您的Datagrid的方法。如果你想清除Datagrid,你只需回想一下这个方法。
这里是我的方法:
public string[] dgv_Headers = new string[] { "Id","Hotel", "Lunch", "Dinner", "Excursions", "Guide", "Bus" }; // This defined at Public partial class
private void SetDgvHeader()
{
dgv.Rows.Clear();
dgv.ColumnCount = 7;
dgv.RowHeadersVisible = false;
int Nbr = int.Parse(daysBox.Text); // in my method it's the textbox where i keep the number of rows I have to use
dgv.Rows.Add(Nbr);
for(int i =0; i<Nbr;++i)
dgv.Rows[i].Height = 20;
for (int i = 0; i < dgv_Headers.Length; ++i)
{
if(i==0)
dgv.Columns[i].Visible = false; // I need an invisible cells if you don't need you can skip it
else
dgv.Columns[i].Width = 78;
dgv.Columns[i].HeaderText = dgv_Headers[i];
}
dgv.Height = (Nbr* dgv.Rows[0].Height) + 35;
dgv.AllowUserToAddRows = false;
}
DGV是DataGridView中的名称
绑定到该控件的数据?请代码! – davenewza
[DataGridView.Clear()](http://stackoverflow.com/questions/3744882/datagridview-clear) –