Here comes the trouble.
I want to delete all rows from datagridview.
This how i add rows:
麻烦来了。我想从datagridview中删除所有行。下面是我添加行的方式:
private void ReadCompleteCallback(object clientHandle, Opc.Da.ItemValueResult[] results)
{
foreach (Opc.Da.ItemValueResult readResult in results)
{
dataGridView1.Invoke(new MethodInvoker(() => dataGridView1.Rows.Add(readResult.ItemName, readResult.Quality, readResult.Timestamp,readResult.Value)));
}
}
And its how i clear gridview:
这就是我清理网格视图的方式:
private void treeView1_SelectionsChanged(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
items = new Opc.Da.Item[treeView1.SelectedNodes.Count];
foreach (TreeNode x in treeView1.SelectedNodes) {
items[treeView1.SelectedNodes.IndexOf(x)] = new Opc.Da.Item();
items[treeView1.SelectedNodes.IndexOf(x)].ItemName = x.Text;
}
group.AddItems(items);
group.Read(group.Items, 123, new Opc.Da.ReadCompleteEventHandler(ReadCompleteCallback), out req);
}
in debug i see that dataGridVIew1.Rows.Count=0, but on form, grid doesnt become clear. what a point?
on each selection in tree, i want to see new rows in table.
在调试中,我看到dataGridVIew1.Rows.Count=0,但在表单上,网格并不清晰。这是什么意思?在树中的每个选择上,我希望看到表中的新行。
更多回答
That seems a very odd time to clear it, and I expect that ties into the issue. What are you trying to do here, for interest?
这似乎是一个非常奇怪的时间来澄清它,我预计这与这个问题有关。你想在这里做什么,为了利息吗?
ok, I see - that makes more sense now. Trying to think why this would behave like that.
好的,我明白了--现在更有意义了。我在想,为什么这件事会是这样。
Why dont you use a DataSource with a BindingList instead?
为什么不使用带有BindingList的数据源呢?
hm.. i dont have datasource. data taken from opc server.
嗯..。我没有数据来源。从OPC服务器获取的数据。
but thats good idea, ill try to do it
但这是个好主意,我会试着去做的
I'm betting you just need to refresh the datagrid. Try this:
我打赌你只需要刷新数据网格。试试这个:
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
If this works... you might want to rethink this part of your application.
如果这行得通..。您可能需要重新考虑应用程序的这一部分。
If I remember correctly, I set the DataSource property to null to clear the DataGridView:
如果我没记错的话,我将DataSource属性设置为NULL以清除DataGridView:
datagridview.DataSource = null;
To clear the datagridview
write the following code:
要清除datagridview,请编写以下代码:
dataGridView1.DataSource=null;
Basically below code line is only useful in data binding scenarios
基本上,下面的代码行仅在数据绑定方案中有用
datagridview.DataSource = null;
otherwise below is the used when no data binding and datagridview is populated manually
否则,在未手动填充任何数据绑定和datagridview时使用下面的
dataGridView1.Rows.Clear();
dataGridView1.Refresh();
So, check first what you are doing.
所以,首先要检查一下你在做什么。
If you want clear a dataGridView not binded to DataSource but manually loaded you can use this simple code:
如果希望清除未绑定到数据源但手动加载的dataGridView,可以使用以下简单代码:
datagridview.Rows.Clear();
datagridview.Columns.Clear();
all you need to do is clear your datatable before you fill it... and then just set it as you dgv's datasource
您所需要做的就是在填充数据表之前将其清除...然后将其设置为您DGV的数据源
I have this piece of code i hope it may help u.
我有这段代码,希望它能帮助你。
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]);
}
catch (Exception exe)
{
MessageBox.Show("You can´t delete A row " + exe, "WTF",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Simply if your are trying to rebind your Data Grid View just code this:
简单地说,如果你试图重新绑定你的数据网格视图,只需编写以下代码:
DataGridView1.DataSource = DataSet.Tables["TableName"];
OR
或
DataGridView.DataSource = DataTable;
else if you trying to to clear your Data Grid View just code this:
否则,如果您尝试清除数据网格视图,只需编写以下代码:
DataGridView.DataSource = null;
DataGridView.Rows.Clear();
DataGridView.Refresh();
and add any method or event to bind Data Grid View Again below this line of code....
并在该行代码下面添加任何方法或事件以再次绑定数据网格视图...
try setting RowCount to 0(allowuserstorows should be false), along with calling clear
尝试将RowCount设置为0(Allowuserstorrow应为False),同时调用Clear
This is one way of doing it:
这是实现这一目标的一种方法:
datagridview.DataSource = null;
datagridview.Refresh();
I hope it works for you because it is working for me.
我希望它对你管用,因为它对我管用。
try:
尝试:
datagrid.DataSource = null;
datagrid.DataBind();
Basically you will need to clear the datasource your binding to the grid.
基本上,您需要清除绑定到网格的数据源。
Try this Method
试试这个方法
dataGridView1.DataSource = dt;
dt.Rows.Clear();
dataGridView1.DataSource=null;
Set the datasource property to an empty string then call the Clear method.
将数据源属性设置为空字符串,然后调用Clear方法。
I don't like messing with the DataSource personally so after discussing the issue with an IT friend I was able to discover this way which is simple and doesn't effect the DataSource. Hope this helps!
我不喜欢亲自摆弄数据源,所以在与一位IT朋友讨论了这个问题后,我发现了这种简单且不会影响数据源的方法。希望这个能帮上忙!
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
cell.Value = "";
}
}
If your DataGridView
does not have any DataSource
the solution does not come by manipulating it.
如果DataGridView没有任何数据源,则解决方案不是通过操作它来实现的。
You will always have an empty
row if you have the AllowUserToAddRows
property set to true
.
如果将AllowUserToAddRow属性设置为True,则始终有一个空行。
Put AllowUserToAddRows = false
if you don't need permise this.
如果不需要许可,请设置AllowUserToAddRow=False。
I know it sounds crazy, but I solved this issue by
changing the datagrid.visible property to false and then to true.
It causes a very small blink, but it works for me (at least for now).
我知道这听起来很疯狂,但我解决了这个问题,方法是将datagrid.Visible属性更改为False,然后更改为True。它会引起很小的眨眼,但对我来说是有效的(至少现在是这样)。
If you would like to keep the DataGridView properties and simply "refresh" it:
如果您想保留DataGridView属性并简单地“刷新”它:
DataTable emptyDt = new DataTable();
dataGridView1.DataSource = emptyDt;
You could encapsulate it in a function for later use.
您可以将其封装在一个函数中以供以后使用。
if you are working with data binging manually the best way to refresh datagridview is dispose the DataTable or Dataset that you used to populate the grid. otherwise how hard you try to clear the rows or remove the rows in Datagridview, it will not refresh the data grid
如果您正在手动处理数据绑定,刷新datagridview的最佳方法是释放您用来填充网格的数据表或数据集。否则,无论您多么努力地清除或移除Datagridview中的行,它都不会刷新数据网格
Try this Code
尝试此代码
private void button_Click(object sender, EventArgs e) {
if (this.dgInv.DataSource != null) {
this.dgInv.DataSource = null;
} else {
this.dgInv.Rows.Clear();
}
}
Try this
尝尝这个
DataGridView1.DataSource=ds;
ds.Clear();
更多回答
this answer assumes use of data binding. If manipulating the grid manually this will not work. Need to do Rows.Clear() and Refresh() for manually inserted rows. See answer by NitroxDM.
这个答案假定使用数据绑定。如果手动操作网格,这将不起作用。对于手动插入的行,需要执行Rows.Clear()和Refresh()。请参阅Answer by NitroxDM。
this answer assumes use of data binding. If manipulating the grid manually this will not work. Need to do Rows.Clear() and Refresh() for manually inserted rows. See answer by NitroxDM.
这个答案假定使用数据绑定。如果手动操作网格,这将不起作用。对于手动插入的行,需要执行Rows.Clear()和Refresh()。请参阅Answer by NitroxDM。
Isn't that what all the other answers are screaming..?
这难道不是所有其他答案都在尖叫的..?
Not exactly. If your data source is bound to your DataTable, then simply calling dataTable.Clear(); will get the job done in one simple step.
不完全同意。如果您的数据源绑定到您的DataTable,那么只需调用dataTable.Clear();就可以在一个简单的步骤中完成工作。
it says that number of rows must be at least 1. but even setting it to 1 - doesnt help.
它规定行数必须至少为1。但即使将其设置为1也无济于事。
this answer assumes use of data binding. If manipulating the grid manually this will not work. Need to do Rows.Clear() and Refresh() for manually inserted rows. See answer by NitroxDM.
这个答案假定使用数据绑定。如果手动操作网格,这将不起作用。对于手动插入的行,需要执行Rows.Clear()和Refresh()。请参阅Answer by NitroxDM。
Iteration is slow. Try calling the row's collection Clear() method instead.
迭代速度很慢。改为尝试调用行的集合Clear()方法。
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
正如它目前所写的,你的答案并不清楚。请编辑以添加更多详细信息,以帮助其他人了解这是如何解决提出的问题的。你可以在帮助中心找到更多关于如何写出好答案的信息。
我是一名优秀的程序员,十分优秀!