Discussion Locked

DataGrid

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
How can I clear or re-initialize the contents of a DataGrid without deleting records from the table? Listbox has .clear method that clears the contents of the listbox. I am trying to do a similar thing with DataGrid. DataGrid.Clearfields did not work for me either.

7 replies

by madestroitsolutions

Well, your description is not clear enough. You have not mentioned what language you are doing it on to begin with, but I can safely guess it is VB, becuase I have encountered the same problem before. If what you want to do is refresh the information shown in the grid, the clearFields method is not going to help, because the only thing it does is reset the grid layout. If you want to refresh the data inside the grid, it depends on how you are using your grid, but the idea is still the same. Yousee, the grid doesnt work like other controls do. Think about the grid as an open window for you to see into a table from a database. If you want to refresh the data inside, you dont refresh the grid, you refresh the recordset. You do this in two ways, depending on the datasource. If the source is an ADO recordset, then simply use the refresh method. Now if the source is a Dataenvironment, then its a little trickier. Rather than rebinding or refreshing, or etc (none of it works, tried it many times) what you do to refresh the recordset on a Dataenvironment is close it, and then reopen it. That is the only way I hve been able to get it to refresh the data (2 years trying now!).

If what you want is to display an empty grid, there is no way I am aware to do so, since it is always bound to a datasource, unless you do one of 2 things: unbind, refresh, then rebind the grid programmatically (dont recommend it), or simply set a SQL statement on the recordset that returns an empty recordset. E.g.: If there is a column that cannot contain 0 as a value, then do "SELECT * FROM Table WHERE Column = 0".

I hope that helps.
Good luck!

by nathwdavis

I am assuming you are talking about using an editable datagrid. I know that there is a method to add a "Cancel" link to your datagrid which undoes any changes you have made and goes back to the uneditable view.

Hope this makes some sense.

by rakeshkanojia

If you have a binded a datasource with the DataGrid then

DataGrid.DataSource = Nothing
DataGrid.ClearStructure

If there is no datasource binding then use
DataGrid.ClearStructure

Alternatively you can also use
DataGrid.Clear

instead of DataGrid.ClearStructure

This discussion is closed to new replies.