Get all Data from ListView and Compare String in C# - c#

I have a ListView in which there are two Columns.
Actually, I want to Get Data From my second Column and then Compare this with my hard Coded message.
E.g At this time, there is a name of Person at first Number in ListView and In Behind Coding, if i have that Specific Name then Count increase and Show me Message that Person Searched.
In Simple words, I want to match the patterns.
It is noted that I am using ListView not GridTableView or Table.
And Sorry about my English, it is not well.

If you want to select the name from the selected row of the list view:
name = listView1.SelectedItems[0].SubItems[1].Text;
If you want to select all results from second column:
foreach( ListViewItem item in listView1.Items )
{
mess += item.SubItems[1].Text.ToString();
}

Related

convert csv items in a list box to a table in mysql database

I am very new to this game and have not got a lot of experience coding, so forgive if this is an odd question.
I have a windows form app in C# that has a child form which reads qr codes and converts the output from a web cam to items in a list box. There are 3 list boxes items from each scan are added to each list box when a button is pushed. The first scan button produces items in list box 1 the second button produces items in list box 2 etc. Because I am trying to fit a lot of information into each list box my first list box produces items separated by commas i.e name,age,sex,id number, the second list box single items (drug) and the 3rd list box single items (quantity).
What i am looking to do is populate a table in MS sql database with each individual item in its own column i.e column1: Name, column 2: age, column 3: sex etc.
Is there a way of converting the listbox1 items from CSV to single words and to add all 3 list box items to a single table?
If it's pseudo-code you want...
Note, there is little DB interaction guidance here...that is way too large a topic.
foreach (var item in listBox.Items)
{
var sa = item.ToString().Split(',');
if (sa.Length == 4) //whatever it ought to be, optional sanity check
{
using (myNewSqlConnection)
using (myNewSqlCommand)
{
//Data type is important here, so pay attention to that
myNewSqlCommand.Parameters.Add("#par1", System.Data.DbType.String).Value = sa[0]; //#par1 would be in the SQL for parameterized query
//Once you've added all of the values, execute the query.
}
}
}

WinForms ListView - get source object from selected string item

I am building a simple language learning helper application in WinForms. One of its modules is a dictionary. It consists of "Sets" and words are stored in a Set. User can create a new Set of words and store some of them in it.
I'm printing all the words from selected one or several Sets in a ListView with columns. Upon checking a Set or Sets in CheckedListBox the List clears and prints words (as string variables).
Trouble comes when there are few Sets checked with their words listed, and user wants to edit one of the listed words. I cannot use indexes (such as index of List item equals to word item in a Set), as those List string items are from different Sets.
Is there any way get a source object from a ListView item? I have not added objects to the list but only some of their variables, but are they somehow connected?
Thank you for your help.
Cheers!
EDIT: Explaining why to me setting a Tag is not a solution:
All the Sets are stored in a List<DictionarySet> dictionarySets. Every Set containts a List<Word> words where words are stored.
How do I fill the ListView:
private void UpdateList()
{
wordsListView.Items.Clear();
List<Word> currentSetWordList;
foreach (DictionarySet ds in setCheckedListBox.CheckedItems) //List<DictionarySets> dictionarySets inserted, DisplayMember set to its Name property
{
currentSetWordList = ds.words;
foreach (Word w in currentSetWordList)
{
ListViewItem newItem = new ListViewItem("--"); //for now a string, later an enum
newItem.Tag = ds;
newItem.SubItems.Add(w.GermanTranslation); //string property
newItem.SubItems.Add(w.PolishTranslation); //string property
wordsListView.Items.Add(newItem);
}
}
}
In this case the program loops through each Set and its word list and prints the words. Their tag is DictionarySet everywhere.
You could use the Tag property on the ListViewItem. The Tag property:
Gets or sets an object that contains data to associate with the item.
In short, when you create each item of the list view, you could add your object as the tag. This way you'll be able to get it back when the user selects an item.
See MSDN for details.

Searching for items in multi column listview in c#

Actually I am creating a playlist view in C#, the Form contains a ListView with 4 columns "Name", "Album", "Artist" and "Path" respectively. I also have a textbox in the Form.
I want to know that how can I search for the items matching the user's search query and find the good possible matching results from all of the columns. For example if a user types the path then how can I get the matching values form path, and if the path contains a name of song then? Actually if you have used any player with playlist like Windows Media Player, when we type in the search query then it shows the possible result. So how can I search for results from all 4 columns. The code currently I am using is only able to search in 1'st column i.e. "Name" column.
Can I replace the textbox with combo box, in which a user can type the search query and maximum of 15 results will be added and shown to the droplist of it. When a user select any item from the dropbox then it should return the path, and the index of that item from listview.
Thank you.
This is an updated answer to the above solution
foreach (ListViewItem item in listView1.Items)
{
if (item.Text == "searchTerm")
{
// do something
}
foreach (ListViewItem.ListViewSubItem subItem in item.SubItems)
{
if (subItem.Text == "searchTerm")
{
// do something
}
}
}

ListBox and CustomTabOffsets.Add

I have created a listBox in a form and I'm trying to find a way to add things like name, phonenumber, city in rows but with each name, phonenumber and city i columns. After some searching I found the CustomTabOffsets.Add, but I don't get it how this work and I can't find any tutorial that explain how this work. Is there anyone here that can help me understand this? I guess there is better options for this, but listBoxs is a must in this task. Thanks!
In my UpdateGUI method I only have this to add a name to the listBox
lstSeats.Items.Add(inName);
You need to use ListView for this. Not ListBox
ListViewItem lvi = new ListViewItem();
lvi.SubItems.Add("SubItem");
listView1.Items.Add(lvi);
A bit too late, but - how to make more than 2 column's in ListBox using C#?
lstSeats.CustomTabOffsets.Add(12); // "The integers represent the number of quarters of the average character width for the font that is selected into the list box."
lstSeats.UseCustomTabOffsets = true;
lstSeats.Items.Add("a\tb); // tab character between the items
The custom tab offset integers are quarter of the average character width for the selected font https://support.microsoft.com/en-us/kb/318601

Display Microsoft Access data in textboxes by selecting an item in a listbox

I know how to connect to an access database and so on.
My question is, I want to select an item from a list box, and then it must search the access database for the item I selected and display all its contents in textboxes. For example:
In the listbox I have the following added:
Car 1
Car 2
Car 3
etc.
If I select Car 2, I want it to read the database and display all Car 2's properties in textboxes. So for example, once I select it, it can display Horsepower in a specific textbox, max speed in a specific text box, year model in a specific text box etc.
Could anyone help me out with this?
Assuming you have 1 textbox per property that will change whenever you select something different from your listbox here is what I would do. It sounds like you are already populating your listbox with some category from your database, possibly CarType. I would use LINQ to run a query to populate the results within the given textbox.
var query = from record in myTable.AsEnumerable()
where record.CarType == myListBox.SelectedValue
select record;
foreach (var record in query)
{
horsePower.Text = record.HorsePower;
//and so on
}
It sounds like you are setting up a custom control for this. However, I would suggest using a Data Grid to do what you are trying to do, instead of Text Boxes.
Have you tried using data binding?
listBox.DataSource = dataTableCars;
listBox.DisplayMember = "car_name";
listBox.ValueMember = "car_id";
txtBox.DataBindings.Add("Text",dataTableCars,"car_name");
txtBox.DataBindings.Add("Text",dataTableCars,"car_driver_name");
etc.

Categories