Microsoft Developer Network >
Forums Home
>
Visual Studio Express Editions Forums
>
Visual C# Express Edition
>
Listview and columns
Listview and columns
- What I'm trying to do is return the value from the 1st row on the 2nd column. I have a listview with 2 columns. I can return the 1st column value buy doing this,
label3.Text = listView1.SelectedItems[0].Text;
So how do I return the 2nd column value.
Toppers BBS http://toppersbbs.dtdns.net
Answers
Hi,
You should use selectedIndexchanged event with the code below.
Here is the code.
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { label3.Text = listView1.SelectedItems[0].SubItems[1].Text; } }
Have you done this?
The last code you pasted is not same with this.- Marked As Answer byJoesoft11a Saturday, November 07, 2009 9:42 PM
All Replies
- try
listView1.SelectedItems[0].SubItems[1].Text; try
listView1.SelectedItems[0].SubItems[1].Text;
Ok, that worked. Just that when I try and select another row. It says invalid index. how can I Select other rows.
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { label3.Text = listView1.SelectedItems[0].SubItems[1].Text; }using the same methed from above.
Thanks
Toppers BBS http://toppersbbs.dtdns.net- Does other row have value in column2?
- Yes it does. the values load from a text file. How ever the problem I'm having is that for some reason. When I add them to the listview. The values seem to skip a line.
Like the 1st one starts at the top. Then when the 2nd one is added. It skips a line and gets added on row 3 and row2 is left blank. I'm been trying to find out what's doing that.
Toppers BBS http://toppersbbs.dtdns.net - Could you send the code, hard to help without seeing the code.
Could you send the code, hard to help without seeing the code.
I'm sorry theres no way to attach files. How ever. I did fix the 1st problem I just can't get the 2nd problem fixed.
In a row. All I want to do is select it and have the value from the 2nd column show in a lebel1.text comtrol
private void Form1_Load(object sender, EventArgs e) { if (!Directory.Exists(Settings1.Default.TextPath)) { Directory.CreateDirectory(Settings1.Default.TextPath); } if (File.Exists(Settings1.Default.TextPath + "TextBases.ixd")) { //LoadBases(); } DirectoryInfo dirr = new DirectoryInfo(Settings1.Default.TextPath); if (File.Exists(Settings1.Default.TextPath + "TextFiles.ixt")) { StreamReader sr; sr = File.OpenText(Settings1.Default.TextPath + "TextFiles.ixt"); string C1,C2; C1 = sr.EndOfStream.ToString(); listView1.BeginUpdate(); while (C1 != null) { ListViewItem Listview1 = new ListViewItem(); C1 = sr.ReadLine(); C2 = sr.ReadLine(); Listview1.Text = C1; Listview1.SubItems.Add(C2); listView1.Items.Add(Listview1); tbases++; } listView1.EndUpdate(); sr.Close(); } //Ends if tbases-=1; label3.Text = "Total Bases " + tbases.ToString(); } private void btn_Addbase_Click(object sender, EventArgs e) { label1.Show(); label2.Show(); textBox1.Show(); textBox1.Focus(); textBox2.Show(); }The code above is how I have it and it works. Now The problem is when it lists intems on each row.
I want to be able to select each row. and have it put the value from the 2nd column in the label3.text.
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e) { label3.Text = listView1.SelectedItems[0].SubItems[1].Text; }The code above is the last thing I tryied before checking any replies.
Thanks for the help
Toppers BBS http://toppersbbs.dtdns.net- Hi,
You should use selectedIndexchanged event with the code below.
Here is the code.
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { label3.Text = listView1.SelectedItems[0].SubItems[1].Text; } }- Proposed As Answer byTamer OzMVPSaturday, November 07, 2009 8:04 PM
- also if that does not work change SubItems[1] to SubItems[0] if you are getting an index error.
OMG, its Joe Ginley! also if that does not work change SubItems[1] to SubItems[0] if you are getting an index error.
OMG, its Joe Ginley!
That didn't work. I change the click to selectedindexchange and it works when I click on the 1st row. When I try and click on the 2nd row. Just error's out and says 0 is not valid for index
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { label3.Text = listView1.SelectedItems[0].SubItems[0].Text; }so it doesn't matter if I use 0 or 1. Just doen't work
any ideas
Toppers BBS http://toppersbbs.dtdns.netHi,
You should use selectedIndexchanged event with the code below.
Here is the code.
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { label3.Text = listView1.SelectedItems[0].SubItems[1].Text; } }
Have you done this?
The last code you pasted is not same with this.- Marked As Answer byJoesoft11a Saturday, November 07, 2009 9:42 PM
Hi,
You should use selectedIndexchanged event with the code below.
Here is the code.
private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { label3.Text = listView1.SelectedItems[0].SubItems[1].Text; } }
Have you done this?
The last code you pasted is not same with this.
Thank You very Much. That worked. And No I never tryied that option.
Toppers BBS http://toppersbbs.dtdns.net

