listview item indexing - c#

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.

Related

Exchange items from one ListBox to another

I am trying to exchange the items in two ListBox controls, but only the word "(Collection)" is added. Why?
You're adding to a ListBox one element which is a collection of elements, ListBox.ObjectCollection.
The ToString() method is called on this object, returning the object type (Collection).
To swap the items, you could use a temporary storage that holds the collection of items of one ListBox, clear its Items collection, add the items of the second ListBox, clear the second ListBox and add the Items of the first one, using the temporary storage as source:
var itemsL1 = new ListBox.ObjectCollection(listBox1, listBox1.Items);
listBox1.Items.Clear();
listBox1.Items.AddRange(listBox2.Items);
listBox2.Items.Clear();
listBox2.Items.AddRange(itemsL1);
This works if the ListBoxes Items collections is not filled using the DataSource property.
Otherwise, swap the data sources.

How to use ensurevisible method in listview?

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.

BindingSource And Composite Properties

I have a method that I call in a form that returns a List and then I throw that into a BindingSource and throw the BindingSource into a DataGridView. The Order class has a property called Items that contains a List. My Order and Item class both implement the INotifyPropertyChanged Interface.
What I'm trying to do is that when a row is clicked in the DataGridView it pulls up the list of items associated with the order from it's Items property into another grid. I want to ensure that those items are 2 way binded to the grid but I can't call it the way I'd like to...
itemsGrid.DataSource = orderBindingSource[orderGrid.CurrentRow.Index].Items;
I can't access the properties of the list from within the BindingSource (After the . operator, Items is not an available option)... Any recommendations? I've tried using a BindingList<Order> and BindingList<List<Order>> but was getting implicit conversion errors. Any suggestions?
*EDIT: Ok I've managed to get the BindingList to work. I didn't know I had to add the list like this
orderList = new BindingList<Order>(Method To Retrieve List<Order>)
Aside from this, the first grid displaying the orders works and updates perfectly. The sub grid which contains the Order's Items bound like
itemsGrid.Datasource = orderList[ordersGrid.CurrentRow.Index].Items;
It displays properly. I don't have editing enabled as I have a few textboxes that are setup and each textbox is bound to the Item from the orderList of the currently selected row of the ordersGrid. I can update the data in the textboxes for the selected item in the subgrid but the changes when I leave the textbox, the sub grid's row for that item property edited does not update as I leave the textbox. It will, however, update if I select another item in the grid. How can I get it to update as I leave the textbox instead of having to click on another row in the grid?

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.

Display the items in DropDownList

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

Categories

Resources