473,387 Members | 1,516 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,387 software developers and data experts.

LivstView Column not aligned with ListViewItem

17
I created the listview as followed:

Expand|Select|Wrap|Line Numbers
  1. ListView lvContent = new ListView();
  2.  
  3. //Add the column header
  4. lvContent.Columns.Add("Col1");            
  5. lvContent.Columns.Add("Col2");
  6. lvContent.Columns.Add("Col3");
  7. lvContent.Columns.Add("Col4");
  8. lvContent.Columns.Add("Col5");
  9.  
Then I have a CSV file with 5 columns. I read the file to populate the rows:

Expand|Select|Wrap|Line Numbers
  1. char[] delim = new char[] {','};
  2.  
  3. System.IO.StreamReader reader = new System.IO.StreamReader(fileName); 
  4. string line;
  5. while ((line = reader.ReadLine()) != null)
  6. {
  7.   String[] arr = line.Split(delim);
  8.   lvi = new ListViewItem();
  9.   for (int i = 0; i < arr.Length; i++)  // arr.Length is always 5
  10.   {
  11.     lvi.SubItems.Add(arr[i]);
  12.   }
  13.   this.lvContent.Items.Add(lvi);
  14. }                    
  15.  
The problem is the column header is not aligned with the data in the listviewitem record. In fact, the columns in listviewitem records is shifted to the right by one position.

Is there anything wrong with my code or it is the nature of CSV file?

Thanks
May 5 '10 #1
2 2156
Plater
7,872 Expert 4TB
Try Trim(), there's probably an extra space in the data
Example "123, 456" the 123 would be "123" and the 456 would be " 456"
May 6 '10 #2
nghivo
17
My problem is what is the first cell in the row goes to the 2nd column, and the 2nd cell goes to the 3rd row, so on. So all the cells in a row shifted to the right by one column.

I found the problem. It has something to do with the ListViewSubItem.
There is a weird relationship b/w all ListViewSubItem objects (in a ListViewItem)and the first ListViewSubItem.

In order to align all cells properly, I changed my code as followed:

Expand|Select|Wrap|Line Numbers
  1. lvsi = new ListViewItem.ListViewSubItem();
  2. lvsi.Text = arr[i];
  3. lvi.SubItems.Insert(i, lvsi);
  4.  
and these lines replace the single line

Expand|Select|Wrap|Line Numbers
  1. lvi.SubItems.Add(arr[i]);
  2.  
So, don't use Add but use Insert.
May 6 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Peter Foti | last post by:
Hi all, I have a form that contains a 2 column table. In the left column is the description for a particular input (ie - "First Name", "Last Name", "Phone Number", etc.). In the right column...
0
by: rmorvay | last post by:
I have successfully integrated sorting in the listview control with the following code: Private Sub ListView_ColumnClick(ByVal sender As Object, ByVal e As...
2
by: MFRASER | last post by:
I am trying to populate a listview and I can not see the column headers, is there a property I am forgetting to set? Here is my code // Create three items and three sets of subitems for each...
3
by: Matthias Kwiedor | last post by:
Hi! I want to display a Tooltip at a ListView, when the Text in a Column won't fit. I know ho to display the Tooltip, but how can i detect if a Text fits into a Column or is cuttet with...
1
by: neelu | last post by:
I have four columns in listview. SR Number Part# DES Qty Weight 59 9410106800 Nut 10 0.03 60 90304ge800 Helmet 5 0.325 61 9635439604 ...
6
by: grs | last post by:
The following is a code example from the Microsoft MSDN. My question is on the following three lines of code: ListViewItem item1 = new ListViewItem("item1",0); ListViewItem item2 = new...
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
4
by: Steph. | last post by:
I have a List view displaying data in Detail mode with several columns. How I can get the column index the user clicked on ? (when user click on an item inside the ListView, not on a column...
1
by: Alan T | last post by:
I have a listview defined a coloumn at design time. And the code I use to add a listviewitem: ListViewItem lvi = new ListViewItem(user.Name); lvi.Tag = (Object)user.Id; ...
1
by: Eran.Yasso | last post by:
Hello all, This is my first time using listview. I was looking in google for a way to write to a specific column. I have 3 columns in my list view. I want to be able to write to a specific...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.