Search code examples
delphirecordsdbgridtlistview

Tlistview - There is any component like Tlistview but with DB access?


I've been trying to make a creative thing to avoid the dbgrids, and i've found the Tlistview (using the one from alphaskins, tslistview), and seems to be a nice way!

The problem is, I don't want to code the event onclick on every tlistview to position a record/dataset according to the item I selected on the tlistview .. and I'm doing it with the tlistview item's caption.. and there could be records with the same names

Here is one of the codes I want to avoid:

with q_find_process do
begin
  close;
  sql.Clear;
  sql.Add('Select * from t_process where process_name like '+quotedstr(streeview1.Selected.Text)+');
  open;
end;

And no, I don't want to put the ID of the Record on the item caption..!

Any ideas?

Does anyone know other way of showing a lot of records without being only text text and more text? I don't know all components on the tool palette, maybe someone could suggest me other one..


Solution

  • I have sometimes used listviews which have been loaded from database tables - only for small amounts of data. I don't understand what you mean by I don't want to code the event onclick on every tlistview to position a record/dataset according to the item I selected on the tlistview, so I'm going to show you how I solved this problem.

    Basically, I create a sub-item which holds the primary key of each record. All the user interface code uses two list views, and at the end, the database is updated. There is no interaction with the database between loading and storing (which might be where I avoid your 'onclick' problem). The widths of each fields are set in the Object Inspector; the final subitem's width is 0 (ie not displayed).

    Loading the list view:

     srclist.items.clear;
     with qSrcList do
      begin
       close;
       params[0].asdate:= dt;  // use date of deposit
       open;
       while not eof do
        begin
         ListItem:= srclist.Items.Add;
         ListItem.Caption:= fieldbyname ('kabnum').asstring;
         ListItem.SubItems.Add (fieldbyname ('price').asstring);
         ListItem.SubItems.Add (fieldbyname ('duedate').asstring);
         ListItem.SubItems.Add (fieldbyname ('docket').asstring);
         ListItem.SubItems.Add (fieldbyname ('id').asstring);
         next
        end;
       close
      end;
    

    Saving data:

     with dstlist do
      for index:= 1 to items.count do
       with qInsert do
        begin
         dstlist.itemindex:= index - 1;
         lvitem:= dstlist.selected;
         parambyname ('p1').asinteger:= deposit;
         parambyname ('p2').asinteger:= strtoint (lvitem.SubItems[3]);
         parambyname ('p3').asfloat:= strtofloat (lvitem.SubItems[0]);
         execsql;
        end;
    

    I hope that this helps you. The context of this code (not that it matters too much) is in a financial application where the user wishes to populate a bank deposit form with cheques. SrcList holds the cheques which have yet to be deposited (there will only be a few per given date) and DstList holds the cheques which have already been connected to a given deposit form.