I am using an LMDGrid and would like to find the best method to clear it and then repopulate it. I can delete the rows one at a time, however this takes a long time. Furthermore, I have tried to do - LMDGrid.Columns.clear and then recreate the columns. This only works as long as I don't select a row, once a row is selected I receive a Unicode error. I am thinking that even with clearing the columns and recreating them, I am also causing other problems with the grid. Is there a way to reset or re-initialize the grid back to its default state, or is deleting each row individual the best method?
As asked below, An LMDGrid is a grid control from LMD Innovative. I am using the non data aware grid.
The code for clearing the grid is
LMDGrid.Columns.Clear; //This clears all of the data and deletes the
//column headers in one shot
//Recreates the column(s)
procedure TForm15.Button1Click(Sender: TObject);
var
cn: TLMDGridTextColumn;
begin
cn := TLMDGridTextColumn.Create(Self);
cn.Title.Caption := 'My column';
cn.Width := 150;
LMDGrid1.Columns.Add(cn);
end;
When run after recreating the tables, it all appears to load the data properly. However, when selecting a row, that's where the problems start as mentioned above.
As far a speed goes when deleting one row at a time through a for loop, even on only 200 records(4sec), it is noticeably slow.
What I am looking for is what most would consider best practice for clearing a grid and repopulating it.
More discovery. I checked what the column count is after doing columns clear, and it reports back as zero. I thought a first that it was only clearing the data under the columns including the header information. I also thought that by recreating the columns, I was duplicating what was already there but not visible. So I have verified that both the columns and row are 0. So I feel that I need to reinitialize the grid component before I insert the new columns, this part I am not sure how to do this.
Here is more on the code.
procedure TContactsForm.GridCreate();
Var
cn1, cn2, cn3, cn4, cn5: TLMDGridTextColumn;
Begin
ContactsGrid.Columns.Clear;
cn1 := TLMDGridTextColumn.Create(Self);
cn1.Title.Caption := 'ID';
cn1.Width := 30;
ContactsGrid.Columns.Add(cn1);
cn2 := TLMDGridTextColumn.Create(Self);
cn2.Title.Caption := 'Company';
cn2.Width := 100;
ContactsGrid.Columns.Add(cn2);
cn3 := TLMDGridTextColumn.Create(Self);
cn3.Title.Caption := 'First Name';
cn3.Width := 85;
ContactsGrid.Columns.Add(cn3);
cn4 := TLMDGridTextColumn.Create(Self);
cn4.Title.Caption := 'Last Name';
cn4.Width := 100;
ContactsGrid.Columns.Add(cn4);
cn5 := TLMDGridTextColumn.Create(Self);
cn5.Title.Caption := 'Phone Number';
cn5.Width := 50;
ContactsGrid.Columns.Add(cn5);
refreshGrid // this repopulates the grid
showmessage('Column Count' + inttostr(ContactsGrid.columns.Count) + 'Row Count' + inttostr(ContactsGrid.DataRowCount))
End;
So what I have noticed is I can recreate the columns as many times as I need, but once a row is selected and recreate the columns it does not create all the columns. The first refresh on a selected row doesn't create the ID column and produces an appropriate error, and then the company column until no columns are created.
Update. When looping through and deleting the rows in reverse or forward, I forgot to add begin update and end update (facepalm). This has greatly increased the performance. Still trying to figure out the columns issue, as within this project it would be nice to use one grid for many purposes.
I received a reply from LMD in response to my questions.
The answers are as followed.
The column issue is: MyGrid.Columns[MyColumnIndex].Free;
And a faster way of clearing the grid is: MyGrid.DataRowCount := 0;