ObjectListView: select subitem programmatically - c#

How can I select a subitem of an ObjectListView programmatically?
SelectObject() and SelectItem() work only with root items, not with subitems.

The things called subitems in an ObjectListView are actually the strings and images that show in the columns.
If you actually want to use the ObjectListView then the most direct way to select a subitem is
objectListView1.Items[index].SubItems[index]
If you use a TreeListView then you should use the method you already found.
Though I recommend selecting and changing in the source instead.

I solve this problem. It can be useful for anybody, who have similar problem. For this, I need to change a source code of control by next:
Change access type for TreeModel property in TreeListView class from protected to public. After this I have access to manipulate Branch objects of TreeListView object. For example, to select any subitem of root element I write next code:
var branch = tlvMain.TreeModel.GetBranch(tlvMain.SelectedObject);
var children = branch.Children.Cast<SecurityObject>().ToList();
tlvMain.SelectObject(children.SingleOrDefault(p=>p.Id == soft.Id));

Related

Add item update listbox

So I have a databound listbox bound to a list produced from my entity:
myListbox.Datasource = myEntity.ToList();
That works fine. My question is, what is the 'correct' way to add a new element to the entity and have it reflected in my listbox?
Currently, I do this:
myEntity.Add(newItem);
myListbox.Datasource = myEntity.ToList();
Surely there is a better way than resetting the datasource each time?
try this, DataBind() will binds a data source to the invoked server control and all its child controls.
myListbox.DataBind()
Please try this:
myListbox.ResetBindings();
See https://msdn.microsoft.com/en-us/library/system.windows.forms.control.resetbindings%28v=vs.110%29.aspx

listview c#: how to add items to columns other than the first

My question is an exact duplicate of this:
"To add items to column 1 in my listView control (Winform) I'm using listView1.Items.Add, this works fine but how do I add items to columns 2 and 3 etc? "
Lots of similar Q&A elsewhere, but none of them talk about how to add items using the WinForms interactive listView builder as opposed to coding it directly. I know how to code it, but since you can add items to the first column using the builder, I assume there must be a way to do it for the other columns.
Right-click on the ListView and select "Edit Items..." to get the ListViewItem Collection Editor.
Select a ListViewItem (or click Add to add one and then select it). In the properties pane find SubItems in the Data category. Click on the "..." button to open the ListViewSubItem Collection Editor and you can add sub-items, which appear in the columns after the first.
You need to set the Text property of your ListViewItems and ListViewSubItems if you want to see anything.
I think (not sure) that most simple way is to add array of strings, and the list view knows the column according to the array index.

How to add nodes to a treeview programatically?

How to add nodes dynamically to a already existing treeview?
if an example as,
-Root
-child1
above one is already existing treeview. but i want to add one more node(child2) to the Root, output is like..
-Root
-child1
-child2
Try this:
TreeNode rootNode = TreeView.Nodes.Cast<TreeNode>().ToList().Find(n => n.Text.Equals("Root"));
if (rootNode != null)
{
rootNode.Nodes.Add("child2");
}
try:
treeView1.Nodes.Add(new TreeNode())
Details are found here: http://msdn.microsoft.com/de-de/library/system.windows.forms.treeview.nodes.aspx
I am assuming you are referring to the asp.net TreeView control
MyTreeView.Nodes.Add(new TreeNode() { Text = "Child 2" });
There are three ways to control a control like a tree view:
Declaratively add values in tags - not an option here
Bind all rows programatically - you can do this, but it is overkill
Add items afterward
TreeviewName.Nodes.Add()
Add to the bound data set
If you are going to have to have the same treeview either a) appear to multiple people or b) reconsitute after postbacks, I actually like massaging and caching the dataset and binding. It is rather simple and lighter weight than the other options if it is being reused.

WPF Listbox.selecteditems returns items in the order they were selected

I was debugging a co-workers program and ran across this issue in WPF.
It looks like the listBoxName.SelectedItems returns a list of selected items, in the order a user selects the item from the interface. This is a problem because I need to preserve the actual order the items.
Example:
the listbox is in Extended selectmode and my listbox contains something: runfirst, runsecond, runthird
the user is given an option to select what they want to run based from the listbox. They select runthird then runfirst. This causes runthird to appear at the top of the list and then runfirst. I guess i could sort the list before running a foreach but i was wondering if there is an easier way.
Thanks
What I did was use LINQ to return the selected items in index order. It is in VB.Net syntax, but it should be easy to fix up for C#.
Dim selecteditems = From selecteditem As ListBoxItem In ListBox1.SelectedItems _
Select selecteditem _
Order By ListBox1.Items.IndexOf(selecteditem)
Yeah I ended up iterating over all the items in the in the listbox and then checked if it was in selecteditems using contains as below
foreach (<yourobject> item in listForSelection.Items)
{
if (listForSelection.SelectedItems.Contains(item))
{
\\code here
}
}
Thanks for the help guys
I think you gave the answer in your question.
Most of the time the order of selected items won't matter. Since it does for you, sorting the selected items before processing them seems to be the simplest solution. I can't really think of a simpler one, especially when the sorting should only add 1 line.
You can use the same Comparison<T> or IComparer<T> that was used to sort the original list. If you're binding to SelectedItems, you can use an IValueConverter to do the sorting.

How to remove selected items from ListBox when a DataSource is assigned to it in C#?

How to remove selected items from ListBox when a datasource is assigned to it in C#?
When trying to remove, got error
"Items collection cannot be modified when the DataSource property is set."
But when i try to remove item from datasource (datatable) ,
it thorws error as "datarow is not in current row collection".
Find that item in the DataSource object and remove it, then re-bind the ListBox.
EDIT:
Here's how you delete from a DataTable as your DataSource, regardless of the .NET version.
DataRowView rowView = listBox.SelectedItem as DataRowView;
if (null == rowView)
{
return;
}
dt.Rows.Remove(rowView.Row);
I haven't tried with anything other than WinForms DataGridViews, but I highly recommend BindingListView, which is both faster than DataTables/Views and allows you to bind generic List<T>s as your DataSource.
Alternatively, use a list that implements IBindingList or inherits from BindingList. When objects are added or removed from a Binding List, any controls bound to it are automatically notified of the change and will update themselves accordingly. If you are using BindingList and your class also implements INotifyProperty changed, Any changes to class properties will also be updated automatically in the databinding control. For example, if a column in a datagrid(view) is bound to a property, "Name", and you change "Name" in the datasource, the datagrid will automatically update. If you add a new item to the datasource, the datagrid will update automatically. Binding List also supports notification in the other direction. If a user edits the "Name" field ina datagrid, the bound object will be updated automatically. Going off topic slightly, if you go a little further and impliment "SupportsSortingCore" and the associated methods in BindingList, you can add automatic sorting to your data. Clicking on a columnm header will automatically sort the list and display the header sort direction arrow.
If the ListBox has a datasource assigned, you must remove items from the datasource and then rebind the ListBox
You need to modify the data source rather than the Items collection of the control. Depending on what kind of data source you are binding to, there are going to be different things you have to do so that your UI updates.
The best way is find a collection that fits your needs and implements IBindingList or IBindingListView. Those two interfaces implement even handlers that listen for a CollectionChanged event and update your UI accordingly.
If your collection doesn't support those interfaces, you're going to have to re-bind your data source every time somebody adds/removes an item.
when you get the message "Items collection cannot be modified when the DataSource property is set."
setting the datasource to something else, empty list or null does not help when
the code initializecomponent is not completed.
to avoid that error, one must do the change of datasource or the item list during or after form load.
I know it does not seem to make sense. Hoever, the visual studio designer will generate code in the form designer.cs or vb that will add items to the listbox if any code that changes the items is found before end of initialize components
While Chris Doggett posted a valid solution, I ran into problems while using it. By using that method it was not allowing a subsequent GetChanges(DataRowState.Deleted) to work properly.
To better solve my problem, I only had to change a single line - the last line.
DataRowView rowView = listBox.SelectedItem as DataRowView;
if (null == rowView)
{
return;
}
rowView.Row.Delete();
This allowed my GetChanges call to work properly.
This worked for me
DataTable temp = (DataTable)lstBlocks.DataSource;
temp.Rows.RemoveAt(position);
its vary simple , assign a new blank value to listbox
eg..
Dim ABC As New List(Of String)()
ListBox1.DataSource = ABC
ListBox implementation is bugged, you need to create a new data source instance for the component for it to recognize a change.
Eg:
ActivitiesList.DataSource = _activities;
_activities = new List<Activity>(_activities);
_activities.Remove((Activity)ActivitiesList.SelectedItem);
ActivitiesList.DataSource = _activities;

Categories