Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion

LMDGrid columns clear question.

I have tried two different methods for clearing or emptying a grid. The first one was a loop, deleting each row one at a time (This was notably slow). The second one was 
calling on the column.clear; in which it did indeed clear it very fast. Then I recreated the columns in code. The problem with the second method was, even though it repopulated the grid, when selecting a row I would get all sorts of errors. 

What else does the column.clear; also effect besides removing the headers? Is there a proper way to clear the row - column data that does not require deleting each row one at a time?

Comments

  • 7 Comments sorted by Votes Date Added
  • You can assing zero to Grid.DataRowCount property:

    MyGrid.DataRowCount := 0;
  • I am already doing this.
    for RowDelete := 0 to ContactsGrid.DataRowCount -1 do
    ContactsGrid.DeleteDataRow(RowCount);

    But it is slow compared to Grid.column.clear;
  • 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.
  • Ok last question what is the best method on removing or deleting a grid column?
  • If you want to delete a column, you can do it like this:

    MyGrid.Columns[MyColumnIndex].Free;

    But, if you simply want to refresh the grid data, then you do not need to clear and recreate columns. As I answered to you previously, to clear grid data you can use DataRowCount property:

    MyGrid.DataRowCount := 0;

  • Thanks for the reply, This is helpful. I started going down a rabbit hole with reusing the grid for various other reasons with in my app.
    I will try this today.
Sign In or Register to comment.