I have a ListView contains 100 items, and we can do certain activities based on selected items. I have used EnsureVisible() method to adjust the visibility and my ListView refreshing time to time to update the data.
The problem now i am facing is if I select the first item in the list, I then start paging down (maybe select the 21th, 59th
and 75th) during this select, I could have highlighted the 1st, 24th and
56th when the control suddenly refreshes the page in this instance, only the 1st selected retains the focus and I have lost my other selections and have to go through again either individually or just quicker.
So my question is how i can select multiple items and do the action while ListView is refreshing during certain interval to fill data though EnsureVisible() is used?
This is my exisitng code:
if (_listviewFirst.SelectedItems.Count > 0)
{
_listviewFirst.SelectedItems[_listviewFirst.SelectedItems.Count - 1].EnsureVisible();
_listviewFirst.SelectedItems[0].EnsureVisible();
}
I would use the ListView.ItemSelectionChanged (MSDN) event and add/remove items (or their references) from a List of selected items. When your control is refreshed you should then iterate your selected items and update the items to selected in your ListView.
You will be able to take advantage of e.IsSelected and e.Item or e.ItemIndex in the event handler to do this.
I wouldn't use EnsureVisible at all for this.
Related
I have a problem with RadListView for C# Winforms that if I add 300 items to the listview the SelectedIndexChanged is firing 300 times.
What can I do to make it stay selected on the first item and not fire every time a new item is added? Because it selects every time the last item.
This is the add code:
this.lstFileList.Items.Add(item);
Instead of adding them one at a time, you can bind directly to an IEnumerable using the DataSource property.
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.
I am using a listview with 6 items per page. When I choose an item from the 6 items it should take me to a page with 3 items per page BUT the selected item (on previous page) as FIRST item in the list on the new page. Basically how do I set which items shows first in the listview? Is that a property?
There is no property for setting the first item. If you are using a datasource, the items will be displayed in the order they occur in the datasource, which is generally the order that the database returns them.
If you need a different ordering, you can code a loop through your datasource and assign the items to the ListView manually.
Hello i have a list view control,
While the form is being loaded i fill the list, i have aprrox. say 100+ items.
While filling i check some parameters and decide which item/row need to be selected.
i set the Selected property to true... refer the code below:
some lines here .....
ListViewItem listViewItem = new ListViewItem("COL1");
listViewItem.SubItems.Add("COL2");
check for some condition and then
listViewItem.Selected = true;
this.m_lstViewCtrl.Items.Add(listViewItem);
This does select the item, there are no issues with it...
however, say the ctrl is sized to see onlu say some 15 items, but the selection is say some 35th item.... currently the scroll bar appears the item is selected but i have to scroll to see what was selected?
is it possible to scroll to the selected item so that is selection is clearly visible...
Will the same apply for a Treeview?
Use the EnsureVisible property on the ListViewItem.
UPDATE: So, your code would be as follows:
listViewItem.Selected = true;
listViewItem.EnsureVisible();
this.m_lstViewCtrl.Items.Add(listViewItem);
In my project i have used four number of dropdownlists in a single form.
when im selecting an item from the 1st dropdownlist,the dropdownlist2 has to display the items that matches the selected item from dropdowmlist1.
Please clear my doubt.,
Use the SelectedIndexChanged-event on the first dropdownlist. Everytime the user selects a different item in the dropdownlist, this event is fired. In the method body you can now react to the change and filter the items of the second dropdownlist to match the selected item of the first dropdownlist. You can either do that manually in a loop or use LINQ (Enumerable.Select).
first of all its called a combobox.
Implement the OnSelectedItemChanged event from the combobox to fill the 2nd combobox
linky