Select a row in listview - c#

I am web developer , working on a part of my project developed in WinForms. So my question could be a basic one. Try to bear with it.
I have two list views on my page and a remove button that works for both.
Problems.
I am not able to select a row in both the list view when I run my program, may be some property needed for it?
If I am able to select the row I want to detect which list view item has been selected, so how would I do that?
I have three columns and have bound the data by using the code below.
listView1.Columns.Add("ID",20);
listView1.Columns.Add("Name",40);
listView1.Columns.Add("Mobile",40);
foreach (var item in dataList)
{
newItem = new ListViewItem();
newItem.SubItems.Add(item.ID.ToString());
newItem.SubItems.Add(item.Name);
newItem.SubItems.Add(item.Mobile.ToString());
listView1.Items.Add(newItem);
}
but the ID column is left blank and the data starts to bind in these sense.
ID Name Mobile
1 abc
2 xyz
So how do I properly show the data?
Lastly I want to use my ID column to delete the data. So if I give width=0, is this the best way to hide a column?

See ListView.FullRowSelect property.
See ListView.SelectedItems property. Note, that by default ListView allows multiselection.
Set item text via constructor: newItem = new ListViewItem(item.ID.ToString());, then add rest of subitems (except of item.ID).
If you want to delete the column, just remove it from the columns collection.

Set
listView1.Items[index].Selected=true;

Related

How to populate one combobox with an SQL dataTable and with an item seperate from that table

Sorry if the title is a bit vague - it is difficult to capture what i want to do in one sentence, so I'll quickly explain what I want to achieve.
I want to bind an SQL table to a comboBox and have all its values (Item names for example) but I also wish to add one of my own items into the same comboBox, separate from the table (For example - "Add new" should then be inserted at the bottom of the list after the SQL table values have been loaded in the comboBox).
Is this possible? If so, how do I do it?
Thanks guys!
You can assign datasource in code-behind like this.
cmb1.DataSource=dt;
cmb1.DataBind();
cmb1.Items.Insert(dt.Rows.Count,new ComboBoxItem("Add New","0"));
cmb1.appenddatabounditems="true";
You can use Insert, you have to specify index and new item to add in list.
cmb1.Items.Insert(tabl.rows.count,"Add new");
OR
ComboboxItem item = new ComboboxItem();
item.Text = "Add new";
item.Value = 0;
cmb1.Items.Add(item);
If you not specify any index it will append at last

ListView not displaying items correctly

What i'm basically trying to do is, to make similar layout in C# Windows Form:
But I've tried like ten times with different codes but nothing seems to work. At last, i tried to create the same layout through the design view, I had my icons stored in a image list and the Image List was assigned to the List View. If i create a group named "BIOS", add a single item named "BIOS Type" and afterwards add a Sub Item "Insyde" ( see the image above ), only the group name is shown, the item and sub item is missing. I basically need to do this through coding, but it won't even work with design view.
My Code:
ListViewGroup group = new ListViewGroup("Computer", HorizontalAlignment.Left);
group.Header = "Computer Info";
listView.Groups.Add(group);
//= new ListViewItem(string name, int ImageIndex, ListViewGroup GroupName);
ListViewItem Item = new ListViewItem("Test", 3, group);
Item.Text = "Check";
listView.Items.Add(Item);
The View Property is set to "Details". This only displays the group header "Computer Info", no items .-.
I would be highly thankful if you anyone can help me with this, I'm stuck with this since 90 minutes -.-
There is a 'View' property to a ListView that can be one of five different settings - make sure that is set to the 'Details' option. (For more information on the different options click here).
You will also need an ImageList if you don't have one already - add images to it and then add it as the SmallImageList property of the ListView to be able to assign items images.
The first column will be the text for your item. For the remaining columns you will need sub-items for each item - in the order that you want them displayed.
In order to show sub items you will need to ensure that there are enough columns in the columns collection to show all of them - one for the title and image of each item and then one each for the sub-items.
Sorry if you knew some of this already, just trying to cover any possible issues you may have. Hope this helps.

How to show subitems below an item in a listview in C#?

Is there a way to show the subitems of an item in a listview below it? I don't want to separate the item in first column and subsequent subitems in subsequent columns, I want it all in a single column and if there were to be more items I want every item separated by a line. Something like this but in C#.
Maybe if it is not possible could it be to make every item like an expandable list?
My code for now is:
lstEvents.View = View.Details;
lstEvents.ForeColor = Color.Black;
lstEvents.Columns.Add("Events");
ListViewItem item = new ListViewItem("Event name");
item.SubItems.Add("23/04/2012");
item.SubItems.Add("$20000");
item.SubItems.Add("this would be the event description");
im using windows forms
In Tile view, each "column" appears as a "row" under the primary column. This will give you what you asked for, but probably not what you want.
You can owner draw the Tile view to produce something like this
(source: sourceforge.net)
You can do this all yourself using just a plain ListView, but the ObjectListView project makes this much easier.
You could subclass the listview and do it yourself.
Or, you can tag certain rows as subrows of the one above it using .Tag = IndexOfPreviousRow and track the relationships in a model. It's kinda hacky, though.
As far as doing it natively with the .NET listview, I don't believe you can.

How To Hide ListView ColumnHeader?

I am struggling to figure out the correct control to use for a list of predefined jobs in the included form. I currently have a ListBoxControl in the Predefined Job Name group that lists all of the predefined jobs for a marine service shop (i.e. oil change, tune up, etc...). Then, based on the item (i.e. job name) that is selected in my ListBox, I need to display the items that correspond to that job. For example, if oil change is the selected job I need to show 4 quarts oil, 1 oil filter, labor, etc...and so on.
Currently, when I load the form data I have a DAO that retrieves all of my jobs from the database using LINQ to SQL. Then I iterate over the results and put the job names into the ListBox. The problem that I am having is that there is no tag for ListBox items like there is for ListView items. So each time the user selects another item in the ListBox, I have to perform another LINQ query to get the job from the database again so that I can display its' corresponding items. If I could use a ListView and hide the column header I could set the entire job on the tag so that each time the user selects a new item I would have access to the details without having to make another call to the database. Is there a way that I can hide the column header of a ListView without hiding the entire column?
You can set the HeaderStyle member of the ListView to None.
listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
Checkout the ListView HeaderStyle property. It has the following options:
None
Nonclickable
Clickable
From MSDN:
The HeaderStyle property allows you to specify whether the column headers are visible or, if they are visible, whether they will function as clickable buttons. If the HeaderStyle property is set to ColumnHeaderStyle.None, the column headers are not displayed, although the items and subitems of the ListView control are still arranged in columns
You can also create simple object like ListItem which has two poperties: Text (string) and Tag (object). Then implement ListItem.ToString() and you can use these in the ListBox as well.
You can also check out Better ListView Express component, which is free and allows displaying items in Details view without columns. The advantage over ListBox and ListView is a native look and many extra features.
Easy way is using the ColumnWidthChanging event
private void listViewExtended1_ColumnWidthChanging(object sender, ColumnWidthChangingEventArgs e)
{
if (e.ColumnIndex == 0)
{
e.Cancel = true;
e.NewWidth = listViewExtended1.Columns[e.ColumnIndex].Width;
}
}
I found that if you know for a fact you are not displaying the headers it may be best to set the HeaderStyle property to None, as Rajesh mentions above.
When setting in the .CS when screen initially loads the headers are displayed until screen is fully rendered.

C# - Update a subitem within a listview

I am working on an application where users enter information that is then added to a listview. That works great. The only problem is, the application connects to a web site that updates the "Points" field in that listview for each account. I am not sure how I can update a single subitem within a listview.
Here is an example screenshot:
How can I select a specific subitem in a specific row to update?
Ok, I'm going to assume Windows Forms.
WinForms' ListViewItem class has a Name property, which you can use to look up a specific item in a list. So as you populate the list, assign a unique value to the Name of each:
var item = new ListViewItem("Text");
item.Name = "foo"; // some unique id string
listView1.Items.Add(item);
That way you can locate the item in the ListView later, using its Items.Find method.
var fooItem = listView1.Items.Find("foo", false);
To expand on Matt's answer, it looks like each row has a unique email address, so you could assign that as the Name property for each ListViewItem. Once you've located the row to update using the Find method, you can update that row's Points like this:
fooItem.SubItems[2] = "450";

Categories

Resources