TMSFMXTableView Adding Item

I have a TMSFMXTableView with several categories. Right now if I add a TableView item to an existing displayed list it is not automatically put in the correct place. 

ex:
MyCategory1 (ID=0)
   Item1
   Item2
MyCategory2 (ID=1)
   Item3

If I add an Item4 and assign it a category ID=0 here is what I get:

MyCategory1
  Item1
  Item2
MyCategory2
  Item3
MyCategory1
  Item4

If I close the app and come back in Item4 is correctly shown under MyCategory1. Can someone point me to what I am doing wrong?  Thanks.


Hi, 


Are you using BeginUpdate / EndUpdate?
Can you provide us a sample, so we can identify what is going wrong?

Kind Regards, 
Pieter

Sure. This is a simple FireMonkey project test. If you click Button1 to add a new item it creates a new duplicate Category 0 with Item 3 in it rather than adding it to the Category 0 that already has 2 items in it.




procedure TForm1.Button1Click(Sender: TObject);  //adds a new item to list
begin
  LogTableView.BeginUpdate;
  try
    with LogTableView.Items.Add do
    begin
      Caption := 'Item 3';
      Description := 'Item Description';
      BulbText := '3';
      Tag := 3;
      CategoryID := 0;  //same CategoryID as 2 items in FormShow
    end;
  finally
    LogTableView.EndUpdate;
  end;
end;


procedure TForm1.FormShow(Sender: TObject);
var
  aTab: integer;
begin
  LogTableView.BeginUpdate;
  try
    for aTab := 0 to 2 do
    with LogTableView.Items.Add do
    begin
      Caption := 'Item '+IntToStr(aTab);
      Description := 'Item Description';
      BulbText := IntToStr(aTab);
      Tag := aTab;
      if aTab=2 then CategoryID := 3
        else CategoryID := 0;
    end;
  finally
    LogTableView.EndUpdate;
  end;
end;





Hi, 


The items are added to categories the way they are added to the collection. So you will need to sort the items when using custom categories.

  finally
    LogTableView.Items.Sort;
    LogTableView.EndUpdate;
  end;

Kind Regards, 
Pieter 

Pieter Scheldeman2014-08-21 08:26:36

You are awesome! Thanks. Knew it had to be something simple I was missing.