开发者

DataGridView.Clear()

Here comes the trouble. I want to delete all rows from datagridview. This how i add rows:

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.It开发者_如何学JAVAem[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.


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:

datagridview.DataSource = null;


To clear the datagridview write the following code:

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

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.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


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


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.


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!

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.

You will always have an empty row if you have the AllowUserToAddRows property set to true.

Put AllowUserToAddRows = false if you don't need permise this.


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).


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜