473,385 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Adding subitems in a ListView

If item is a ListViewItem and str is a string, why do the following two lines
not have the same effect ?

item.SubItems.Add(new ListViewItem.ListViewSubItem()).Text =
str;
item.SubItems.Add(str);

In the example code that follows, using the first line (in the ListView
constructor) causes the later redisplay of values (to some specified number
of decimal places) in the ListView not to work. Using the second line causes
the redisplay to work as expected.

I am using C#.NET 2003, Standard Edition.

Any ideas ?
Thanks, Andrew.

// example code

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Modify
{
/// <summary>
/// Summary description for Form1.
/// </summary>

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Label label1;
private double[] X = {0.13579, 0.02468}; // values to display

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
ConstructColumns();
BuildList(); // causes Modify() not to work correctly.
//BuildList1(); // ok
// why are BuildList() and BuildList1() not equivalent ?

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listView1 = new System.Windows.Forms.ListView();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.n umericUpDown1)).BeginInit();
this.SuspendLayout();
//
// listView1
//
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(56, 32);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(144, 97);
this.listView1.TabIndex = 1;
this.listView1.View = System.Windows.Forms.View.Details;
//
// numericUpDown1
//
this.numericUpDown1.Location = new System.Drawing.Point(144, 152);
this.numericUpDown1.Maximum = new System.Decimal(new int[] {
12,
0,
0,
0});
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(48, 20);
this.numericUpDown1.TabIndex = 2;
this.numericUpDown1.Value = new System.Decimal(new int[] {
2,
0,
0,
0});
this.numericUpDown1.ValueChanged += new
System.EventHandler(this.numericUpDown1_ValueChang ed);
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 152);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 23);
this.label1.TabIndex = 3;
this.label1.Text = "Decimal Places";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(240, 197);
this.Controls.Add(this.label1);
this.Controls.Add(this.numericUpDown1);
this.Controls.Add(this.listView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.n umericUpDown1)).EndInit();
this.ResumeLayout(false);

}
#endregion

private void ConstructColumns()
{
// sets up two columns in ListView1
HorizontalAlignment centre = HorizontalAlignment.Center;
listView1.Columns.Add("A",70,centre); // why is first column not centred ?
listView1.Columns.Add("B",70,centre);
}

public void BuildList()
{
string fmt = "{0:F" + (int) numericUpDown1.Value + "}";
for (int i = 0; i <= 1; i++) // two rows
{
// build row
ListViewItem item = listView1.Items.Add(i.ToString()); // first column
item.SubItems.Add(new ListViewItem.ListViewSubItem()).Text =
String.Format(fmt,X[i]); // second column

}
}

public void BuildList1()
{
string fmt = "{0:F" + (int) numericUpDown1.Value + "}";
for (int i = 0; i <= 1; i++) // two rows
{
// build row
ListViewItem item = listView1.Items.Add(i.ToString()); // first column
item.SubItems.Add(String.Format(fmt,X[i])); // second column
}
}
public void Modify()
{
// change # decimal places used in column 2.
// does not remove rows or columns : just changes subitem.Text for second
column
string fmt = "{0:F" + (int) numericUpDown1.Value + "}";
foreach (ListViewItem item in listView1.Items)
{
int i = listView1.Items.IndexOf(item); // row index starting from zero
ListViewItem.ListViewSubItemCollection list = item.SubItems;
foreach (ListViewItem.ListViewSubItem subitem in list)
{
if (list.IndexOf(subitem) > 0) // column index starting from zero
{
subitem.Text = String.Format(fmt,X[i]);
}
}
}
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)
{
Modify(); // does not work if BuildList() used in constructor of Form1;
works if BuildList1() used.
}

}
}

Nov 16 '05 #1
0 12228

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Eva | last post by:
Hi, I wanted to know how i can enter values into a specific column of a listview. I have tried the following code but this seems to enter all my values into the first column!!! Can anyone...
1
by: Mark Doggett | last post by:
Hi I have a listview control with four columns. The problem I have is that the second column needs to display an image but as it is a subitem this doesn't appear to be able to be done. Does...
2
by: cybertof | last post by:
Hello, Is there a solution to the following problem : When filling a listview (30 columns) with around 5000 items, it can take easily 10 sec for the listview to be filled. I have used...
0
by: Samuel R. Neff | last post by:
I'm having a index problem with ListView SubItems. If I add multiple columns to the listview and then add items with associated subitems, the ListView displays fine. Then if I delete a column via...
20
by: Ash Phillips | last post by:
Hi Everyone, I have this program I wrote in VB6 for family use. It's a DVD Database just for me to keep track of them cause I have so many lol. In VB6, I could add items to the ListView in...
6
by: Jack | last post by:
Hello, I've noticed through searching this group's previous posts that one can get the item the mouse is over in a listview control but I did not see how to get the subitem the mouse is over. Is...
0
by: Jack | last post by:
Hello all, In code, I'm updating the text property of subitems in a Listview which is in Details view. This may be a dumb question, but I can't seem to find a good way to refresh the listview to...
0
by: sonu | last post by:
Hi all, I have a listview control which has four columns like { Col1, Col2, Col3, Col4}. I add data in listview like this, dim lvitem as listviewItem
2
by: MikeY | last post by:
Hi everyone, After reading various posts I'm still scratching my head and unsure of what approach to take. I have created buttons that upon clicking, the buttons add an item name (myName) to my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.