Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListView: Adding and Removing Columns on the Fly

Status
Not open for further replies.

cyprus106

Programmer
Apr 30, 2001
654
I've got a ListView with 4 existing columns. When I create the data for the LV, in runtime, from a table, I add 7 subitems worth of data, and just don't have those extra four columns. The user can't actually see the last 4 subitems of data. The ListView is in vsReport style... Now I want to be able to show/add and remove/hide columns on the fly. Say I've got these columns VISIBLE:
Name
Phone Number
Tag
Total

And the 'invisible Columns', there's:
Vehicle
Labor Price
Purchases
Date

I'd SWEAR I've done this before but I can't find ANY code on how... Maybe I'm just blind, but I fail to see a method of doing this! My explaination may be a bit confusing, so here's my code to add an item to the list:

Code:
ListItem = CustListView->Items->Add();
ListItem->Caption = /* customer's name */        
/* these are visible from the ListView */
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("cust_phone")->AsString);        ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("tag")->AsString);        ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("total")->AsString);        
/* these are not */
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("vehicle")->AsString);
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("labor")->AsString);
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("num_purchases")->AsString);
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("date")->AsString);

MainForm->CtTable1->Next();

Now, in the ListView, there's only 4 columns that I've created in design time: Name, Phone, Tag, Total... Now I want to be able to hide and show those and the other 4, Vehicle, Labor, Purchases, and Date in runtime. I hope I explained this well enough. Thanks a lot for any help!!

Cyprus
 
I'm not sure, but the TListView Column(in VCL) doesn't have a property Visible.
 
You're right, I beleive I've seen it done somewhere. I'll look around this weekend and see if I can dig it up.


James P. Cottingham
-----------------------------------------
[sup]To determine how long it will take to write and debug a program, take your best estimate, multiply that by two, add one, and convert to the next higher units.[/sup]
 
A bad way to do that would be by setting the width property to "0" and the Autosize to false.
 
Yeah, that's an awfully sloppy thing to do, and probably wouldn't work, since when the user tries to change the width of any remaining columns, it'll pull out the 'hidden' column. And I can't make the widths fixed, since I need to be able to resize them accordingly. I've looked around. I would swear I saw somethign about this... I just can't find it!

Cyprus
 
Why are u using the TListView Component?
You can use the DBGrid intead of the ListView

That way you can set the query to show just the columns that you wanna see
 
I have to use the ListView. There's a lot more code than what I was requesting an answer to.

Cyprus
 
I've been wondering, why define
Code:
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("vehicle")->AsString);
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("labor")->AsString);
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("num_purchases")->AsString);
ListItem->SubItems->Add(MainForm->CtTable1->FieldByName
("date")->AsString);
until you really need them?

James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
I grabbed this from the help files.
Ive messed with it some but it seems to be
doing what you are asking. I havent used
TListVeiw so am unfamiliar with the details.

Code:
{
  const char Names[6][2][10] =
   {{"Rubble","Barny"},
    {"Michael", "Johnson"},
    {"Bunny", "Bugs"},
    {"Silver", "HiHo"},
    {"Simpson", "Bart"},
    {"Squirrel", "Rocky"}};

  TListColumn  *NewColumn;
  TListItem  *ListItem;
  //TListView   *ListView = new TListView(this);

  //ListView->Parent = this;
  //ListView1->Align = alClient;
  ListView1->ViewStyle = vsReport;
  NewColumn = ListView1->Columns->Add();
  NewColumn->Caption = "Last";

  NewColumn = ListView1->Columns->Add();
  NewColumn->Caption = "First";
  for (int i = 0; i < 6; i++)
  {
    ListItem = ListView1->Items->Add();
    ListItem->Caption = Names[i][0];
    ListItem->SubItems->Add(Names[i][1]);
  }
  }
hope it helps

tomcruz.net
 
I really need to add and remove them on the fly. Say, a user want's to get rid of the 'vehicle' column, then i've got to shift the columns behind that one over. So far, there seeems to be no way of doing this.

Cyprus
 
ListView1->Columns->Delete(0);

you got to do the shift and resize the remaining
columns Im sure but that line above has remove
columns on a short test project with the add code
above it is a start. as with a stringgrid you
pretty much have to restructure the object and
shift the data manually each time.

tomcruz.net
 
That's the way it looks. LEt me know if you find out anything more, please. Thanks!

Cyprus
 
This works for me:

TListItem *list_item=ListView->Items->Add();
list_item->Caption="name";
list_item->SubItems->Text=property->GetItem(field_index+1);
list_item->Update();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top