ListView not displaying items correctly - c#

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.

Related

Use DataBinding to maintain the alphabetic short between listBoxes

I've been programming some little project in Visual Studio using c# (.NET FRAMEWORK) and in some of my windows forms I want to show in different lisboxes the information about some object.
In my program I got a class named Client that has some Properties: an int called DNI (this is the one that identifies a Client), a string called Name and an int called Telephone. I want a form to show 3 different listboxes, each one with the list of elements of a collection of Client objects. The point is that a Client object has it's Properties showed in the same index of each listBox (to be said, reading horizontally you see the three values of the properties). I have implemented a button in top of each one that alphabetically sorts the elements of the listBox, but I would like to make a Binding between them so When you alphabetically sort one listbox, the other two sort their elements to match the new order of elements in the one alphabetically sorted.
I've been told the class DataBindingscan be used to do this. I've tried searching on the internet about it but achieved nothing and reading the documentation didn't help either, so I ended up posting it here. How can I use DataBindings to solve this? Any help or hint will be appreciated, thanks in advance.
What must I write to bind the indexes of them?
I recommended a datagridview; you seem to have tabular data that you want to show in a tabular fashion.. However, we'll demo all of it.
Easiest route:
Make a new winforms project
Add a DataSet type of file, open it, right click the design surface and choose Add .. DataTable
Name the table Client
Right click it and add column, call it DNI, use the props grid to make its type Int32
Add a string column Name
Add an int column Telephone
Save, switch to Form1 designer
Open Data Sources window (View menu, Other Windows)
Drag the Client node onto the form. A datagridview appears along with some stuff in the tray. Remove the navigator; it's not so useful in this context
Add 3 listboxes to the form
For each listbox:
Use the props grid to set its DataSource to the bindingsource
Set the DisplayMember to a different column - one DNI, the other Name and the third Telephone
That's it for now, run the app. Type some data in the datagridview. Note that when you sort by clicking the DGV header, the listboxes follow; this is because the sort instruction is being carried out by the bindingsource, and all controls bind through the bindingsource:
So that's thngs bound through a bindingsource to a strongly typed datatable, which is conceptually not really any different to what you have. The bit of work making the table and its columns in the design surface is the notional equivalent of making a class with properties. Indeed if you were to open the DataSetBlahBlah.Designer.cs file you'd find a class ClientRow with 3 properties DNI, Name, Telephone
You could change up everything you've made to be based on e.g. a List<Client> but your sorting life becomes slightly more awkward because a BindingSource doesn't understand how to sort it. As such you'd end up with something like:
//class property
private List<Client> Clients = new();
//in constructor
clientBindingSource.DataSource = Clients;
//in button that does sorting:
clientBindingSource.DataSource = Clients.OrderBy(c => c.name); //etc
Personally I'd leave it as a strongly typed datatable; for what you'd want out of a List<Client> a ClientDataTable collection of ClientRow are surface similar enough..
Clients.Where(c => c.Name == "John"); //how you might search a List<client>
ClientsDT.Where(c => c.Name == "John"); //how you might search a ClientDataTable
Clients.Add(new Client(){ DNI = 1, Name = "a", Telephone = 1 } ); //add to a List<Client>
ClientsDT.AddClientsRow(1, "a", 1); //how you might add to a ClientDataTable
etc
If you have already implemented a button for each, you've almost finished.
Every ListBox have to be bound with a List of Client, showing a specific property as DisplayMember.
The button of each ListBox can sort the List and refresh all of the ListBoxes.
If you want more information, please post some of your code.

DropDownList Showing First Item Empty

Every time this Dropdown is showing first Item Blank
ComboBox cb;
List<string> namesCollection=new List<string>();
namesCollection.Add("---- Select ----");
namesCollection.Add("ABC1");
namesCollection.Add("ABC2");
namesCollection.Add("ABC3");
namesCollection.Add("ABC4");
foreach(string pname in namesCollection)
cb.Items.Add(pname);
Does anyone have solution for this ?
You appear to be defining the ComboBox right there in your code, so I'll assume it's actually displayed somewhere in your form / window.
It's normal for the ComboBox to display a blank line initially.
Specify the item you want to display, immediately after populating the ComboBox with data:
cb.SelectedIndex = 0;
You probably have a line feed in your items collection. Hard to see. Adding a cb.Items.Clear() before populating is probably a good idea anyway and will get rid of the problem, if you can't locate it.

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.

Select a row in listview

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;

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.

Categories

Resources