By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,951 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

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

P: 3
Hi,

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.

Basically

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.

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

Thanks for any replies,

-

Stuart
Sep 8 '07 #1
Share this Question
Share on Google+
3 Replies


kenobewan
Expert 2.5K+
P: 4,871
Where are you storing/ updating the information? This would normally be the place to get the updated info. HTH.
Sep 9 '07 #2

P: 3
Hello,

Thanks for the reply, all help is appreciated.

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

name
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.

e.g.

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.

-

Stuart
Sep 10 '07 #3

P: 3
Hi

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
  1.  
  2. if (lvEmployeeDetails.SelectedItems.Count == 0) return;
  3.  
  4.             ListViewItem selectedItem = lvEmployeeDetails.SelectedItems[0];
  5.  
  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;
  12.  
  13.             selectedRecord = lvEmployeeDetails.SelectedItems[0].Index;
  14.  
  15.  
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;
  3.  
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???

-

Stuart
Sep 10 '07 #4

Post your reply

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