Advertisement

03.15.2008 at 09:49AM PDT, ID: 23244145
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

How to access columns created in a ListView box

Tags: Visual Studio .Net 2005 -  C++
Greetings:

I have a Visual C++ .Net Windows Form application with a listView box on it.  This box successfully creates four columns:

Form1::listView1->View = View::Details;
Form1::listView1->GridLines = true;
Form1::listView1->Columns->Add("Column 1", 70,HorizontalAlignment::Left);
Form1::listView1->Columns->Add("Column 2",70,HorizontalAlignment::Left);
Form1::listView1->Columns->Add("Column 3",70,HorizontalAlignment::Left);
Form1::listView1->Columns->Add("Column 4",70,HorizontalAlignment::Left);

The problem I'm having now is how to access individual columns.  How do I identify a column and then add a string value to it?

Thanks
Start your free trial to view this solution
Question Stats
Zone: Microsoft
Question Asked By: John500
Solution Provided By: Dr-Hussain
Participating Experts: 1
Solution Grade: A
Views: 32
Translate:
Loading Advertisement...
03.15.2008 at 11:11AM PDT, ID: 21133662

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 11:31AM PDT, ID: 21133738

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 11:57AM PDT, ID: 21133821

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 12:33PM PDT, ID: 21133950

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 12:42PM PDT, ID: 21133982

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 12:48PM PDT, ID: 21134004

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 01:18PM PDT, ID: 21134105

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 01:19PM PDT, ID: 21134110

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 01:22PM PDT, ID: 21134115

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 01:54PM PDT, ID: 21134232

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 02:02PM PDT, ID: 21134268

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 02:38PM PDT, ID: 21134399

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 02:52PM PDT, ID: 21134449

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 02:54PM PDT, ID: 21134458

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.15.2008 at 03:08PM PDT, ID: 21134504

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
03.16.2008 at 07:23PM PDT, ID: 21139507

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
03.15.2008 at 11:11AM PDT, ID: 21133662
Hi,

you need to use a key in adding the columns :
Form1::listView1->Columns->Add( key_String, text_String, width_Int );

then you can use the key used to add the column to identify it and access it again , this way:
ColumnHeader c = Form1::listView1->Columns[ key_String ] ;

you can also use the index like this?
ColumnHeader c = Form1::listView1->Columns[ 0 ] ;  // to get the first column you added


and now you can change the header title again:
c.Text = "NewValue";
 
03.15.2008 at 11:31AM PDT, ID: 21133738
Dr,

Thanks, good stuff.

Although I can see the need to change a header title down the road some time, can you add the lines which show how I would insert data into a particular column?

Let's say I have the following:

Form1::listView1->Columns->Add( key_1_String, text_String, width_Int );
Form1::listView1->Columns->Add( key_2_String, text_String, width_Int );

ColumnHeader c = Form1::listView1->Columns[key_1_String];

How is data inserted into the column entitled Key_1_String ?

Thanks!

 
03.15.2008 at 11:57AM PDT, ID: 21133821
to insert items into the listview you don't need to reference the columns ..
you  can add items like this:

listView1->tems->Add(new ListViewItem(new string[] {"c1_value","c2_value","c3_value", "c4_value"}));


Accepted Solution
 
03.15.2008 at 12:33PM PDT, ID: 21133950
Ok, understood.

I'm getting error that goes like this:

Error 1 error C2440: 'initializing' : cannot convert from 'const char [9]' to 'std::string (*)[]'
Error 2 error C2078: too many initializers
Error 3 error C2664: 'System::Windows::Forms::ListViewItem::ListViewItem(System::String __gc *)' : cannot convert parameter 1 from 'std::string (*)[]' to 'System::String __gc *'      

Form1::listView1->BeginUpdate();
Form1::listView1->Items->Add(new ListViewItem(new string[] {"c1_value","c2_value","c3_value", "c4_value"}));
Form1::listView1->EndUpdate();

Previous to adding columns my routine looked like this:

void Form1::ListView1Msg(string strValue)
{

Form1::listView->BeginUpdate();
String *ListViewData = __gc new String(strValue.c_str());
Form1::listView->Items->Add(ListViewData );
Form1::listBox5->EndUpdate();

}

Hence, the strValue would hold data enough for four columns.   Or, I could pass anything necessary like a 'String' - MyStrArray [4] or a 'string'  - MyStrArray[4].

At any rate, I don't know how to proceed because when I compare your method with what I've been doing, I don't know whether I have to pass delimeters in a string an array.  Just what will this listView take in terms of parameters if I don't have to specifically reference column headers?

Thanks
 
03.15.2008 at 12:42PM PDT, ID: 21133982
maybe the error is because of the syntax .. I am not sure about this .. I am using c# .. and I tried just to change the "." to "->" to convert it to c++ syntax ..

anyhow .. here is the concept .. every time you add new item (row) you need to define an array of string that hold values for each column you defined ..if you don't know what value you should submit for a certain column at this time ..you can supply an empty string for that column


regarding your routine ... you don't need to update the viewlist ... beside ..
you called the "BeginUpdate" of a listview and then you called the "EndUpdate"
of a different control with is listbox5 .. this is wrong ..and you don't have to do it anyway,
 
03.15.2008 at 12:48PM PDT, ID: 21134004
I hear you.  I'll fool around with it.  the listBox5 was a copy and paste error, but it's not in the code.

Regarding the Begin and Update, I'm almost certain I needed to do it this way because of the function type.  The data was being passed to the listView but it wasn't ever being seen at run-time:

static void ListView1Msg(string strValue)

I'll try to figure out how to pass the array .....
 
03.15.2008 at 01:18PM PDT, ID: 21134105
No option seems to be working.  I don't know if there is a C# method equal to C++.  On a different post, the person says all this is necessary:

----------------------------------------------------------------------
listView1.View = View.Details;
listView1.GridLines = true;
listView1.Columns.Add("Column 1", 100,HorizontalAlignment.Center);
listView1.Columns.Add("Column 2", 100, HorizontalAlignment.Center);
listView1.Columns.Add("Column 3", 100, HorizontalAlignment.Center);
ListViewItem parent1 = new ListViewItem("PARENT1 Item for line 1");
ListViewItem parent2 = new ListViewItem("PARENT2 Item for line 2");

ListViewItem.ListViewSubItem child1 = new ListViewItem.ListViewSubItem(parent1, "SubItem 1 for line 1");
ListViewItem.ListViewSubItem child2 = new ListViewItem.ListViewSubItem(parent1, "SubItem 2 for line 1");

parent1.SubItems.Add(child1);
parent1.SubItems.Add(child2);
listView1.Items.Add(parent1);

child1 = new ListViewItem.ListViewSubItem(parent2, "SubItem 1 for line 2");
child2 = new ListViewItem.ListViewSubItem(parent2, "SubItem 2 for line 2");

parent2.SubItems.Add(child1);
parent2.SubItems.Add(child2);
listView1.Items.Add(parent2);
--------------------------------------------------------------------------

In the entire sample above I see no place where a particular column is referenced.....
 
03.15.2008 at 01:19PM PDT, ID: 21134110
I still say there has got to be a way to access & update an individual column.  Why have columns if they couldn't be referenced specifically?
 
03.15.2008 at 01:22PM PDT, ID: 21134115
try to add complete items ..not parent and childs


just like this :
listView.Items.Add(new ListViewItem(new string[] {"c1_value","c2_value","c3_value", "c4_value"}));

this is easier
 
03.15.2008 at 01:54PM PDT, ID: 21134232
Any idea how I can access the ListViewSubItem objects?

The method you suggest above just isn't working unless you have some insight as to why I'm getting the errors I mentioned.

I did some reading in the docs and it says:

A ListView control displays a list of items that are defined by the ListViewItem class.  Each ListViewItem can store subitem objects that are defined by the ListViewItem.ListViewSubItem class.  Subitems are displayed when the View property of the ListView control is set to Details (did it).  Typically, subitems contain information that is related to their parent item.  For example, if a ListView control displays items that represent orders, each item could display the order number.  Subitems could be added to each item to display iinformation such as the product ordered, the quantity of items ordered, and the total price....+

Do you ever use the SubItem approach?
 
03.15.2008 at 02:02PM PDT, ID: 21134268
u can use this approach :

listView1.Items[0].SubItems[0].Text = "some value";
 
03.15.2008 at 02:38PM PDT, ID: 21134399
That didn't work either but here's what I found in the docs.  My code doesn't include a class.  Is that what I need to create?  If not, what am I missing in terms of accessing SubItems as you described last post?

void CreateMyListView()
   {
      // Create a new ListView control.
      ListView* listView1 = new ListView;
      listView1->Bounds = Rectangle(Point(10,10),System::Drawing::Size( 300, 200 ));

      // Set the view to show details.
      listView1->View = View::Details;

      // Allow the user to edit item text.
      listView1->LabelEdit = true;

      // Allow the user to rearrange columns.
      listView1->AllowColumnReorder = true;

      // Display check boxes.
      listView1->CheckBoxes = true;

      // Select the item and subitems when selection is made.
      listView1->FullRowSelect = true;

      // Display grid lines.
      listView1->GridLines = true;

      // Sort the items in the list in ascending order.
      listView1->Sorting = SortOrder::Ascending;

      // Create two items and three sets of subitems for each item.
      ListViewItem* item1 = new ListViewItem( "item1",0 );

      // Place a check mark next to the item.
      item1->Checked = true;
      item1->SubItems->Add( "1" );
      item1->SubItems->Add( "2" );
      item1->SubItems->Add( "3" );
      ListViewItem^ item2 = new ListViewItem( "item2",1 );
      item2->SubItems->Add( "4" );
      item2->SubItems->Add( "5" );
      item2->SubItems->Add( "6" );

// Create columns for the items and subitems.
      listView1->Columns->Add( "Item Column", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 2", -2, HorizontalAlignment::Left );
   
 //Add the items to the ListView.      LOST ME HERE
      array<ListViewItem*>*temp1 = {item1,item2};
      listView1->Items->AddRange( temp1 );

// Add the ListView to the control collection.
      this->Controls->Add( listView1 );
}






 
03.15.2008 at 02:52PM PDT, ID: 21134449
the code you have looks fine !

if you have c# installed also you can try the my code ..it is working fine .. just create new form , add a button and a list view ..and paste this code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
 private void Form1_Load(object sender, EventArgs e)
        {
            listView1.View = View.Details;
 
            ListViewItem item = new ListViewItem(new string[] {"c1_value","c2_value","c3_value"});
         
            listView1.Columns.Add("key1", "c1", 100);
            listView1.Columns.Add("key2", "c2", 100);
            listView1.Columns.Add("key3", "c3", 100);
 
                    
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
           
            listView1.Items[0].SubItems[0].Text = "new c1 value";
 
        }
Open in New Window
 
03.15.2008 at 02:54PM PDT, ID: 21134458
sorry ..i missed one line ..here is it again:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
  {
            listView1.View = View.Details;
 
            ListViewItem item = new ListViewItem(new string[] {"c1_value","c2_value","c3_value"});
         
            listView1.Columns.Add("key1", "c1", 100);
            listView1.Columns.Add("key2", "c2", 100);
            listView1.Columns.Add("key3", "c3", 100);
 
            listView1.Items.Add(item);
 
                    
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
           
            listView1.Items[0].SubItems[0].Text = "new c1 value";
 
        }
Open in New Window
 
03.15.2008 at 03:08PM PDT, ID: 21134504
Appreciate the code.  I'll definitely try it tomorrow, I'm running out of time here today.  I need to learn C# .... I'll probably take this entire project and attempt to create it in C# just understand the differences.

When I tried to compile the code I showed you from the docs in my last post  - I get errors on this line:

 array <ListViewItem*>*temp1 = {item1,item2};

Error 1 error C2065: 'array' : undeclared identifier            
Error 2 error C2059: syntax error : '>'

'array' shows up in blue as a reserved system word so I don't know why there would be a problem.  It isn't declared any where in the example.  I'm guessing I don't have a particular library included??  I would think all the System Form inclusions would do it..........
 
03.16.2008 at 07:23PM PDT, ID: 21139507
Doc,

Sorry for the slow response.  I ended up using the array approach as you said.  Hence, I was able to figure out what array types would satisfy the requirement.


void Form1::ListView1Msg(string AircraftData[5])
{
for(int i = 0; i < 5; i++)
      ListViewArray[i] = __gc new String(AircraftData[i].c_str());

Form1::listView1->BeginUpdate();
ListViewItem *ListViewData = new ListViewItem(ListViewArray);
Form1::listView1->Items->Add(ListViewData);
Form1::listView1->EndUpdate();
return;
}

If you can tell me how to update a particular row that would be great.  If it's the first  row I can see how that would be easy.   What if it was the 2nd row and I didn't want to touch the other rows.  Would this require rewriting all the rows regardless?

Thanks!
 
 
03.16.2008 at 07:33PM PDT, ID: 21139530
Hi again,

you can modify any single row at anytime without modifying the rest of the rows .. just use the index of that row .. I hope it is clear now ?
 
 
03.20.2008 at 10:23AM PDT, ID: 21173564
Doc,

Here's another semi-related question concerning a listBox:

http://www.experts-exchange.com/Microsoft/Development/.NET/Visual_CPP/Q_23257485.html

thanks
 
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628