473,836 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member

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(record toupdate).text = blahblah etc
listview(record toupdate).subit em(1).text = blahblah etc

Thanks for any replies,


Sep 8 '07 #1
3 11641
4,871 Recognized Expert Specialist
Where are you storing/ updating the information? This would normally be the place to get the updated info. HTH.
Sep 9 '07 #2
3 New Member

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
3 New Member

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

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

Similar topics

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 particular row that is double clicked. Can someone give me an example of how to get at the item, and subitems, of a selected row in a listview? Thanks, billy
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 deactivate and reactivate the form (an MDI child, in case that matters), the SelectedIndexChanged event is fired. On its own, that's not a problem... but the following code causes trouble:
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 MultiSelect to False. The first click goes as planned, but all subsqequent clicks seem to fire the event twice. The first firing triggers an ArgumentException, resulting in nItem not being initialised. That's why I have the first
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 what if I want values from other cols?? I also may want to find the position of the row selected. In listbox I use the selectindex, how would i do this in listview?
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 hearder..) Thanks for any help !
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
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 (string), i want the index of the fField arraylist. i just have a 0 in there at this point...i would think there is something similar to indexing a listbox, such as lvProductSelection.SelectedIndex
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 Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
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? Thanks, Gidi
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.