Question:
Creating an array from items in a list view C# Help?
?
2012-07-07 11:19:42 UTC
Hello guys,

I know i've been asking a lot of questions lately about C# coding But this should hopefully be the last one.

Okay So I have this list view which contains the details of some teachers, it has these subitems:
Name
Class
Username
Password

The list view is currently filled with teachers and I need a way of copying those that are selected from the list view to an array so I can do some processing with them.

I currently have the following Teacher class setup which works perfectly:

public class Teacher
{
public string TeachersName
{ get; set; }
public string TeachersClass
{ get; set; }
public string TeachersUsername
{ get; set; }
public string TeachersPassword
{ get; set; }
}

I need and array of teachers:
Teacher[] Variablename = new Teacher[lv_Teachers.SelectedItems.Count];

That is the current declaration i have for it.

Please can someone help me with the processing to copy all the selected items into the array.

Thanks!!
Four answers:
Bob M
2012-07-07 15:28:10 UTC
Your current method for putting items into List is almost fine, I'm guessing you typed it wrong because you were typing in here rather than in the editor.



List teachers = new List();



void AddTeacher()

{

Teacher tmpTeacher = new Teacher();

tmpTeacher..TeachersName = "Fred Blogs";

tmpTeacher.TeachersClass = "History";

tmpTeacher.TeachersUsername = fredb";

tmpTeacher.TeachersPassword = "xyz123";



teachers.Add(tmpTeacher);

}



Showing the data in a listview - Your function is fine



If you want an array of the items in the list then use, but you don't need this as I'll explain in a moment.

teachers.ToArray();



When ever you put data you want feedback from into a view (ListView or TreeView) then make use of a tag, just a simple integer ID so when you pull selected items from your List list instead of a convoluted read of items and sub items.



Start in your class andding one more property -



Public Class Teacher

{

public int tagid;

...

}



In the function where you add teachers we make this change -



int iIDCounter = 0;



void AddTeacher()

{

Teacher tmpTeacher = new Teacher();

//****************************

tmpTeacher.tagid = iIDCounter++;

//****************************

tmpTeacher..TeachersName = "Fred Blogs";

tmpTeacher.TeachersClass = "History";

tmpTeacher.TeachersUsername = fredb";

tmpTeacher.TeachersPassword = "xyz123";



teachers.Add(tmpTeacher);

}





This means that when you go through your selected items you only need to read the ListViewItem tag, then use Linq to collect the items from your data -



List tagIDs = new List();



Foreach(Teacher tmpTeach in lv_Teachers.SelectedItems)

{

tagIDs.Add( (int)tmpTeach.tag);

}



That is it, your tagIDs contains a list of the IDs from teachers that are selected.



Foreach(int tmpID in tagIDs)

{

Teacher thisTeacher = teachers.Select(n => n.tagID == tmpID);

// do something with this record

}



Since have the tagID, if you need to write something back to the listview, you can easily find the one record and alter that. Much faster updates with no flickering.
Ratchetr
2012-07-07 12:07:40 UTC
When you populate the list view, are you working with Teacher objects? Assuming you are, then one simple approach is to save the teacher object in the ListViewItem. You can use the Tag property for that. Tag can be used to store any object.



So if you are filling a ListViewItem from a variable named teacher, just add:

item.Tag = teacher;



Now you can get to the original Teacher object from the selected item. One way to do that is with a for loop:



Teacher[] teachers = new Teacher[lv_Teachers.SelectedItems.Count];



for (int i = 0; i < lv_Teachers.SelectedItems.Count;i++ )

    teachers [i] = lv_Teachers.SelectedItems[i].Tag as Teacher;



Another way to do it is to take advantage of extension methods:

var teachers =

lv_Teachers.SelectedItems.Cast< ListViewItem>( ).Select(it=>it.Tag as Teacher).ToArray();



Either approach should give you an array of teachers from the selected items.
Forn1972
2017-03-03 18:33:08 UTC
1
cornatzer
2016-12-08 15:07:05 UTC
expensive, its your college or college project proper ? you may desire to do your self. Actully i'm additionally scholar of C++ OOP, data platforms, in final semester i've got been given all solved assignments from my fellows and on the top :) whilst tests got here i've got been given a programme to code and that i became like :( so . in this software you are going to be able to desire to apply. #contain #contain #contain make a sort for storing all clientele data shopper type then make gadgets for ever shopper, then make a sort for products type products, type clientele, then use gadgets here memory might desire to allocate dynamically for data using New Operator yet once you opt to apply Array its your determination. its elementary to do . proper of success :) you may desire to seek on C++ educational web content for help dont get complete code from some one you will study no longer something.. Actully expensive once you pass some the place in bus na you are able to by no skill be attentive to the all way precisely and cant pass returned there by using your self on your vehicle . yet whilst as quickly as you pass on your vehicle some the place na what in case you pass with too lots issues yet after attaining first time next time u will by no skill might desire to invite bus driving force the way :)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...