Working with listviews

C

CSharp-Jay

Hey all, I am really having the worst time trying to figure out the
way the listview control works. Basically as a side project I am
working on an Address Book, and in it I have a listview control with
the obvious columns(First Name, City, State, etc). When the program
is closed the data gets saved to a comma delimited file called
data.dat. When the program is opened data.dat is automatically read
into a string array line by line which is then supposed to add each
array element into its appropriate column in the listview. But I
cannot get anything to show up on the control >< The program doesn't
crash and bug checks show it's breaking up the array appropriately,
but still a blank listview stares at me. I have to admit this is like
my second time using it and well...I am at a loss right now, I will
submit a code snippet but mind you it is probably a wee bit jacked by
now since I can't figure out how to get this done.

ListViewItem items;

sw = new StreamWriter("data.txt", true);
sw.Close();
sr = new StreamReader("data.txt");
string line;
string[] lines;
while ((line = sr.ReadLine()) != null)
{
lines = line.Split(',');
items = new ListViewItem();
for (int i = 0; i < lines.Length; i++)
{
if (i == 0)
items.Text = lines;
else
items.SubItems.Add(lines);
}
}
sr.Close();
 
C

CSharp-Jay

Just noticed a little bit of misinformation in my original post. The
program will ultimately write/read from data.dat, but while I am
programming it I am using data.txt, so unfortunately it's not that I
am reading from one file and writing to another =\
 
D

Dom

Just noticed a little bit of misinformation in my original post.  The
program will ultimately write/read from data.dat, but while I am
programming it I am using data.txt, so unfortunately it's not that I
am reading from one file and writing to another =\

Hey all, I am really having the worst time trying to figure out the
way the listview control works.  Basically as a side project I am
working on an Address Book, and in it I have a listview control with
the obvious columns(First Name, City, State, etc).  When the program
is closed the data gets saved to a comma delimited file called
data.dat.  When the program is opened data.dat is automatically read
into a string array line by line which is then supposed to add each
array element into its appropriate column in the listview.  But I
cannot get anything to show up on the control >< The program doesn't
crash and bug checks show it's breaking up the array appropriately,
but still a blank listview stares at me.  I have to admit this is like
my second time using it and well...I am at a loss right now, I will
submit a code snippet but mind you it is probably a wee bit jacked by
now since I can't figure out how to get this done.
            ListViewItem items;
            sw = new StreamWriter("data.txt", true);
            sw.Close();
            sr = new StreamReader("data.txt");
            string line;
            string[] lines;
            while ((line = sr.ReadLine()) != null)
            {
                lines = line.Split(',');
                items = new ListViewItem();
                for (int i = 0; i < lines.Length; i++)
                {
                    if (i == 0)
                        items.Text = lines;
                    else
                        items.SubItems.Add(lines);
                }
            }
            sr.Close();- Hide quoted text -


- Show quoted text -


You're never adding items to the listview. I don't have my CSharp
handy, but I think it's something like:

ListView1.Items.Add (items)
 
P

Peter Duniho

CSharp-Jay said:
[...] The program doesn't
crash and bug checks show it's breaking up the array appropriately,
but still a blank listview stares at me. I have to admit this is like
my second time using it and well...I am at a loss right now, I will
submit a code snippet but mind you it is probably a wee bit jacked by
now since I can't figure out how to get this done.

I don't see anything in the code you posted that actually adds the new
ListViewItem to your ListView.

That could be your problem. Or it could be you simply have not provided
enough code. If it's not your problem, post a concise-but-complete code
example so that we can see _everything_ relevant to the question.

Pete
 
C

CSharp-Jay

CSharp-Jay said:
[...] The program doesn't
crash and bug checks show it's breaking up the array appropriately,
but still a blank listview stares at me.  I have to admit this is like
my second time using it and well...I am at a loss right now, I will
submit a code snippet but mind you it is probably a wee bit jacked by
now since I can't figure out how to get this done.

I don't see anything in the code you posted that actually adds the new
ListViewItem to your ListView.

That could be your problem.  Or it could be you simply have not provided
enough code.  If it's not your problem, post a concise-but-complete code
example so that we can see _everything_ relevant to the question.

Pete

Sadly that is the whole code :( Quite apparently I have no clue what
I am doing lol.
 
H

Helmut Giese

Hi,
as others have noted: You create your items but don't do anything with
them - you certainly don't hand them over to the ListView.
ListViewItem items;

sw = new StreamWriter("data.txt", true);
sw.Close();
sr = new StreamReader("data.txt");
string line;
string[] lines;
while ((line = sr.ReadLine()) != null)
{
lines = line.Split(',');
items = new ListViewItem();
for (int i = 0; i < lines.Length; i++)
{
if (i == 0)
items.Text = lines;
else
items.SubItems.Add(lines);
}

add here
yourListView.Items.Add(items);
}
sr.Close();

Not related to your question but nonetheless: I found your naming of
variables - well - confusing:
- The variable holding _one_ item is called 'items' (how about
'item'?).
- You split _one_ line and call the parts 'lines' (how about 'parts'
or 'tokens' instead)?
Of course, living in a free country you can do it any way you like :)
Good luck
Helmut Giese
 
C

CSharp-Jay

Hi,
as others have noted: You create your items but don't do anything with
them - you certainly don't hand them over to the ListView.
           ListViewItem items;
           sw = new StreamWriter("data.txt", true);
           sw.Close();
           sr = new StreamReader("data.txt");
           string line;
           string[] lines;
           while ((line = sr.ReadLine()) != null)
           {
               lines = line.Split(',');
               items = new ListViewItem();
               for (int i = 0; i < lines.Length; i++)
               {
                   if (i == 0)
                       items.Text = lines;
                   else
                       items.SubItems.Add(lines);
               }


add here
                yourListView.Items.Add(items);
           }
           sr.Close();

Not related to your question but nonetheless: I found your naming of
variables - well - confusing:
- The variable holding _one_ item is called 'items' (how about
'item'?).
- You split _one_ line and call the parts 'lines' (how about 'parts'
or 'tokens' instead)?
Of course, living in a free country you can do it any way you like :)
Good luck
Helmut Giese


Alright so I modified the code a bit per everybody's assistance.
Still the problem exists, at this point I've stepped into all the code
and verified that before going to add a new line it loads the next
string from the file. It also is now adding to the rows but in the
first column ONLY. It appropriately loads the first bit of data
before the first comma into the appropriate row in the appropriate
column. But it still leaves all the subitems blank, any ideas?

ListViewItem item;

sw = new StreamWriter("data.txt", true);
sw.Close();
sr = new StreamReader("data.txt");
string line;
string[] lines;
while ((line = sr.ReadLine()) != null)
{
lines = line.Split(',');
item = new ListViewItem();
for (int i = 0; i < lines.Length; i++)
{
if (i == 0)
lstContacts.Items.Add(lines);
else
item.SubItems.Add(lines);
}
}
sr.Close();
 
C

CSharp-Jay

Hi,
as others have noted: You create your items but don't do anything with
them - you certainly don't hand them over to the ListView.
           ListViewItem items;
           sw = new StreamWriter("data.txt", true);
           sw.Close();
           sr = new StreamReader("data.txt");
           string line;
           string[] lines;
           while ((line = sr.ReadLine()) != null)
           {
               lines = line.Split(',');
               items = new ListViewItem();
               for (int i = 0; i < lines.Length; i++)
               {
                   if (i == 0)
                       items.Text = lines;
                   else
                       items.SubItems.Add(lines);
               }


add here
                yourListView.Items.Add(items);
           }
           sr.Close();

Not related to your question but nonetheless: I found your naming of
variables - well - confusing:
- The variable holding _one_ item is called 'items' (how about
'item'?).
- You split _one_ line and call the parts 'lines' (how about 'parts'
or 'tokens' instead)?
Of course, living in a free country you can do it any way you like :)
Good luck
Helmut Giese


Okay so I can now vouch that it is adding rows and populating the
correct values into the first column. But the subitems are still not
loading and regardless of how many times I search google I come up
with this whole "ListViewItems itm1 = new ListViewItems(); deal. But
how does ListViewItems know to add the subitems to lstContacts?
Obviously it doesn't because it isn't working and I just cannot figure
it out, please help!

private void frmMain_Load(object sender, EventArgs e)
{
ListViewItem item;

sw = new StreamWriter("data.txt", true);
sw.Close();
sr = new StreamReader("data.txt");
string line;
string[] lines;
while ((line = sr.ReadLine()) != null)
{
lines = line.Split(',');
item = new ListViewItem();
for (int i = 0; i < lines.Length; i++)
{
if (i == 0)
lstContacts.Items.Add(lines);
else
item.SubItems.Add(lines);
}
}
sr.Close();
}
 
C

CSharp-Jay

Hi,
as others have noted: You create your items but don't do anything with
them - you certainly don't hand them over to the ListView.
           ListViewItem items;
           sw = new StreamWriter("data.txt", true);
           sw.Close();
           sr = new StreamReader("data.txt");
           string line;
           string[] lines;
           while ((line = sr.ReadLine()) != null)
           {
               lines = line.Split(',');
               items = new ListViewItem();
               for (int i = 0; i < lines.Length; i++)
               {
                   if (i == 0)
                       items.Text = lines;
                   else
                       items.SubItems.Add(lines);
               }

add here
                yourListView.Items.Add(items);
Not related to your question but nonetheless: I found your naming of
variables - well - confusing:
- The variable holding _one_ item is called 'items' (how about
'item'?).
- You split _one_ line and call the parts 'lines' (how about 'parts'
or 'tokens' instead)?
Of course, living in a free country you can do it any way you like :)
Good luck
Helmut Giese

Okay so I can now vouch that it is adding rows and populating the
correct values into the first column.  But the subitems are still not
loading and regardless of how many times I search google I come up
with this whole "ListViewItems itm1 = new ListViewItems(); deal.  But
how does ListViewItems know to add the subitems to lstContacts?
Obviously it doesn't because it isn't working and I just cannot figure
it out, please help!

        private void frmMain_Load(object sender, EventArgs e)
        {
            ListViewItem item;

            sw = new StreamWriter("data.txt", true);
            sw.Close();
            sr = new StreamReader("data.txt");
            string line;
            string[] lines;
            while ((line = sr.ReadLine()) != null)
            {
                lines = line.Split(',');
                item = new ListViewItem();
                for (int i = 0; i < lines.Length; i++)
                {
                    if (i == 0)
                        lstContacts.Items.Add(lines);
                    else
                        item.SubItems.Add(lines);
                }
            }
            sr.Close();
        }


w00t I just finally got it! 24 hours of working on it pays off in the
long run *grumble* (microsoft please investigate the poorly designed
c# code for listview useage). Anyways the final code was this:

sw = new StreamWriter("data.txt", true);
sw.Close();
sr = new StreamReader("data.txt");
string line;
string[] lines;
while ((line = sr.ReadLine()) != null)
{
lines = line.Split(',');
ListViewItem lvi = new ListViewItem(lines);
lstContacts.Items.Add(lvi);
}
sr.Close();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top