c# listview: Get selected subitem - c#

I have a ListView with 9 columns and an unknown number of rows.
In two different columns, the user shall be able to click (or double-click) on a subitem and I need to know which of these two subitems in this row was chosen.
So I need to know either which SubItem of a ListView has been chosen or in which column it was.
Thank you!
Tom

Related

c# listbox numerical order of items

Hello I'm searching throught internet for hours but still I can not find the answer. (or I just don't know how to ask)
I need a simple numerical order of items in listbox fe. I am adding every 3rd items to listbox from txt file.
When I have this in txt file:
Acc 4
name4
pass4
Acc 5
name5
pass5
I'll add to listbox only Acc4 and Acc5
And now when I select some item in listbox I want to know which one is selected but I don't want fe.: Acc6 (is selected) but I want to know Acc6is 3nd one in listbox (then order is 2)
PS: sorry for my end and thx for help
If you are looking for the index of the selected item in a listbox try
{listBoxName}.SelectedIndex
This will give you the index (0 based) of a selected item. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.selectedindex?view=netframework-4.7.2
If you want the item try
{listBoxName}.SelectedItem
This gives the currently selected item https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox.selecteditem?view=netframework-4.7.2

ListView; Sort & Highlight columns

I was wondering 2 things.
How can I make it so when I click on a row it selects both columns?
How can I make it so it auto sorts them when they click a column?
If C# doesn't allow this then is there any libs I could use or anything easier? Thanks.
If you mean your listview has 2 columns and you want to select the both of them on row selection, you can just set FullRowSelect=true.
For your second question check this Sorting ListView Items by Column Using Windows Forms

Split a listview into multiple columns

I am populating a ListView with a DataTable from the code behind. The problem is, that the ListView is becoming too long with too many items.
I am wondering, how can I make this:
Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Into this:
Item 1 Item 4
Item 2 Item 5
Item 3 Item 6
Maybe I'm overlooking something, but I can't seem to find the answer.
Please not that I want to keep it in one ListView, so I'm not looking to split the items and put them into multiple ListViews.
Solution: Use a DataList in stead of a ListView. See Tim's answer.
Use a DataList instead. It has a RepeatDirection and RepeatColumns properties. The former would be RepeatDirection.Vertical and the latter 2 in your case.
Gets or sets the number of columns to display in the DataList control.

Assigning the selected values of checkboxlist items to listbox

I am developing a small business application to display the report retrieved from database and I have a checkboxlist consisting of four checkboxes, also a combobox. Now, I am retrieving the required info from database to combobox with 4 items while on page load, which is ok. Not only this, but also retrieving the checkboxlist DataTextField on the checkboxes from database,which is also ok.
As for my question on this issue, lets assume that I have a combobox with 2 items, name and age and also 4 checkboxes named with George, Michael, 23, 33.
Here, 23 and 33 belong to Name(combobox.selectedindex[0]) items of checkboxes and George, Michael belong to the Age item of combobox. Then, I want to display on the listbox that when clicking Name item of combobox, checkboxes of Michael and George come to the screen as checked automatically and when clicking the button, those selected values of checkboxes are displayed on the listbox for both nMme and Age items of combobox in the same logic.
I tried to make this several times, but fail to achieve this goal. How to do this, can you please lead the way to me on my this issue?
Thank a a lot for replying.
The simple way would be to add Name or Age as checkBoxListItem.Tag to the corresponding values and everytime you change selection in the combobox you iterate through all the checkBoxItems and chack the ones with the corresponding tag and uncheck the others.

C#. How do I fill ListView fully? All Items and all Subitems

everyone. How do I fill ListView fully at once.
For example, I click some button and my LisView fills fully, all Items and all SubItems that I have.
I was also confused by this when I started using System.Windows.Forms.ListView (I'm assuming that is the ListView you're referring to).
Essentially, each row in the ListView corresponds to a single ListViewItem in the .Items collection. The text of the item appears in the first column of the ListView. When the ListView is in details mode (.View = View.Details), then the SubItems collection is used, which is a property of each ListViewItem.
The pattern can be seen in the example code for ListView on MSDN:
ListViewItem item1 = new ListViewItem("item1",0);
item1.SubItems.Add("1");
item1.SubItems.Add("2");
item1.SubItems.Add("3");
ListViewItem item2 = new ListViewItem("item2",1);
item2.SubItems.Add("4");
item2.SubItems.Add("5");
item2.SubItems.Add("6");
listView1.Items.AddRange(new ListViewItem[]{item1,item2});
I'm assuming you mean the .NET ListView?
You can change the View Property to 'Details' and that will show all items and subitems.
The SubItems get populated from second column onwards, if the listview has columns. The first column is taken by the top level item.

Categories

Resources