473,405 Members | 2,171 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,405 software developers and data experts.

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

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
3 11607
kenobewan
4,871 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
sstein
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
sstein
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

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

Similar topics

2
by: billyb | last post by:
My listview has two columns: Email Address & Email Address Type. I've figured out how to populate it, but now I'm having trouble figuring out how to properly use the SelectedItems to get at the...
0
by: Graham | last post by:
Hi everyone I have a form which contains a ListView control. The code handles the SelectedIndexChanged event, and changes some icons on some items in the ListView. I've found that when I...
3
by: n. Smith | last post by:
Hi All, Is it normal that the ListView fires the selectedinexchange event twice? I have a LvLoaners list view item that updates 3 text boxes (code below), when I click on an item. I have set...
1
by: Jonesgj | last post by:
Hi, First time I am using this control in lieu of a listbox, and I want to be able to get values and row positions back ... I seem to be able to get the first col value back using focusitem, but...
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...
6
by: George | last post by:
Hi all, How can I get the value stored from the selected item and subitems of a listview? Thanks in advance, George
2
by: forest demon | last post by:
in a different app, i can grab the index of a ListBox, but when i try it with a listview with only one column, i'm having issues. the first line (string) below works as expected. the second line...
0
by: =?Utf-8?B?TmFjaA==?= | last post by:
It works on the first selection when I want to get a different selection I get InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index The Following is the code Private...
2
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, I'm trying to get the index of a selected Item in ListView. I'm using the SelectedItemChange Event, but i can't get the item index (like in listbox for i.e). How can i get the index? ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.