467,876 Members | 1,218 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,876 developers. It's quick & easy.

C# - Getting the index of the selected item in a listview.


I am having a bit of trouble with some C# code. I can get it working in VB.NET, but can't seem to work out how to get the same code working in C#.

I have a listview which stores information about employees.


Day, Hours Worked, Hours Off, Absence Reason, Subs, Pay for day.

The listview allows you to select a list and the details from the list are put into various textboxes to allow them to be edited. However, I can't work out how to then re-write those details back to the listview, updating the original record.

I can get it working by looping around each item in the listview, and checking against original data stored then updating but its a messy way of doing it. There must be a way to get the index of the item that is being updated. Then e.g.

listview(recordtoupdate).text = blahblah etc
listview(recordtoupdate).subitem(1).text = blahblah etc

Thanks for any replies,


Sep 8 '07 #1
  • viewed: 11048
3 Replies
Expert 4TB
Where are you storing/ updating the information? This would normally be the place to get the updated info. HTH.
Sep 9 '07 #2

Thanks for the reply, all help is appreciated.

Basically I have a file for each employee. The file stores e.g.

holiday day entitlement

then the lines for each day they work, including the hours on, hours off, absence type (if any), money subbed etc in a comma delimited list.

The list of employees is read into a combox on a form, I select an employee and it reads in their data and puts each day into a row on the listview. I can click on the listview and that record info gets put into some textboxes.

The problem is updating the original listview with the updated data.


I click on row 6 for 01/01/2008 and change the hours on from 8 to 10. I want to then be able to click on update and the data for row 6 will udpate the appopriate subitem. I can then dump the entire listview back to the CSV file by overwriting the original. There is only ever going to be at max a few hundred lines in the file so performance isn't an issue. I can't seem to find a way to know which row number has been updated in the listview.


Sep 10 '07 #3

I have solved this.

I used a variable in the class called SelectedRecord. Then when I click on an item in the listview it executes the following code.

Expand|Select|Wrap|Line Numbers
  2. if (lvEmployeeDetails.SelectedItems.Count == 0) return;
  4.             ListViewItem selectedItem = lvEmployeeDetails.SelectedItems[0];
  6.             dateDay.Text = selectedItem.Text;
  7.             txtHoursWorked.Text = selectedItem.SubItems[1].Text;
  8.             txtHoursOff.Text = selectedItem.SubItems[4].Text;
  9.             txtSubs.Text = selectedItem.SubItems[2].Text;
  10.             txtDaysPay.Text = selectedItem.SubItems[5].Text;
  11.             cmbReasonForAbsence.Text = selectedItem.SubItems[3].Text;
  13.             selectedRecord = lvEmployeeDetails.SelectedItems[0].Index;
Then to update the record.

Expand|Select|Wrap|Line Numbers
  1. lvEmployeeDetails.Items[selectedRecord].Text = dateDay.Text;
  2.             lvEmployeeDetails.Items[selectedRecord].SubItems[1].Text = txtHoursWorked.Text;
This seems to work, although I am not entirely sure why.

I though selectedItems[0].Index would always return 0 as multi-select is set to false. I guess it doesnt return the index of the group of selected items???


Sep 10 '07 #4

Post your reply

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

Similar topics

2 posts views Thread by billyb | last post: by
reply views Thread by Graham | last post: by
1 post views Thread by Jonesgj | last post: by
reply views Thread by =?Utf-8?B?TmFjaA==?= | last post: by
2 posts views Thread by =?Utf-8?B?R2lkaQ==?= | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.