I am using the DevExpress 9.3 CheckedComboBoxEdit, and I need to get the collection of all checked items. It seems like this should be a simple task, but the closest thing I have found to a solution is something that says I can use:
CheckedComboBoxEdit.Properties.GetItems.GetCheckedValues()
Unfortunately, there is no GetCheckedValues method here. I have found the following:
CheckedComboBoxEdit.Properties.GetCheckedItems()
which returns an object, but I cannot find any reference on what I should cast the object as. I have also tried to iterate through the items, and check each one to see if it is checked, following the suggestion from here, but Items returns a collection of Strings, not CheckedListBoxItem, so I cannot test if they are checked.
What I want is a String collection of checked items; right now, I am okay to receive them as any type of collection, or even create the collection myself. I know there must be some very simple thing that I am overlooking, but I can't seem to find it.
SOLUTION
This is the solution that I came up with. I would prefer something more elegant; it seems like there should be a way to get the checked items, since this is what the control is for. Nevertheless, this seems to work:
Private Function GetChecked() As List(Of String)
Dim checked As New List(Of String)
Dim checkedString As String = CType(SitePickerControl.Properties.GetCheckedItems(), String)
If (checkedString.Length > 0) Then
checked.AddRange(checkedString.Split(New Char() {","c}))
End If
Return checked
End Function
If anyone can give me a proper solution, I would love to see it.
This is what I use:
var ids = (from CheckedListBoxItem item in checkedComboBoxEdit.Properties.Items
where item.CheckState == CheckState.Checked
select (int)item.Value).ToArray();
You can also make an extension method on CheckedListBoxItem which will return only checked items values.
(It's C#, not VB, but the concept is the same.)
I know this is an old post, but I thought I should pitch in anyway.
I'm not sure on when v9.3 was released, but there definitely is a GetCheckedValues() function now. It is described here:
https://documentation.devexpress.com/#WindowsForms/DevExpressXtraEditorsControlsCheckedListBoxItemCollection_GetCheckedValuestopic
and I also found it as the answer in one of their support cases (which is much older than this post) here:
https://www.devexpress.com/Support/Center/Question/Details/Q431364
So to get a list of all the selected values you need something like:
myCombo.Properties.GetItems().GetCheckedValues()
or to check if a specific value was selected:
if (myCombo.Properties.GetItems().GetCheckedValues().contains("myvalue"))
I hope this helps future searchers.
For VB.NET
Dim ids = (From item In checkedComboBoxEdit.Properties.Items Where item.CheckState = CheckState.Checked Select CInt(item.Value)).ToArray()
This is what I use:
var ids = (checkedComboBoxEdit1.Properties.Items.Where(m => m.CheckState == CheckState.Checked)).Select(m => m.Value).ToArray();
Related
Following a tutorial, i've built a LINQ query to filter a List of objects:
Dim Query = From FilteredItem In Items, FiltSupp In FilteredItem.Suppliers
Where ((SuppliersComboBox.SelectedItem.ToString = "All Suppliers" OrElse FiltSupp.CompanyName = SelectedSupplier) And
(String.IsNullOrEmpty(TxtItemCode.Text) OrElse FilteredItem.ItemCode.ToString.Contains(TxtItemCode.Text)) And
(String.IsNullOrEmpty(TxtName.Text) OrElse FilteredItem.Name.ToLower.Contains(TxtName.Text.ToLower)) And
(String.IsNullOrEmpty(TxtPkgCost.Text) OrElse FilteredItem.PkgCost.ToString.Contains(TxtPkgCost.Text)))
Select FilteredItem
The problem is that the query returns nothing even when the list is being populated.
At debugging, the list value is correct (populated with few items), but the query doesn't returning anything. I can't figure out what is wrong, so any suggestion would be greatly appreciated!
Ps. I'm using vb.net, but answers in C# are also wellcome
To start off with, this answer is obviously not complete yet, so please hold off any downvotes until I finalize it. I have every intention of helping the OP if I can, however at this time there is no way for me to even begin helping out because the information needed is not present.
#ISAE, Can you please provide the following:
hoping the Items being queried is a strongly typed DataTable; the class file for the Items object type, preferably the designer files also (.xsc, .xsd, and .xss)
the forms' designer files and code behinds.
Alternatively, if the project can be put on GitHub, that would be even better. If you can't release all of that code, then really need to know enough to somewhat reconstruct what's going on. As one of the comments pointed out, that LINQ query is really messy and it's hard to dissect right now.
Ok, I'm totally at a loss with this....
I think I have looked at every example and every code snippet around and still can't work out how to implement a custom sorter on my ObjectListView.
My primary column (column 0) contains numbers only (as a string) but is sorting all items by alphanumeric instead of numeric, meaning that it is doing something like this:
1
11
111
2
22
etc.
I am trying to find a relatively simple example of how to modify my ObjectListView to sort column 0 when it first loads, but I'm struggling.
I have converted over a custom class called ColumnSorter (from CodeProject) into VB and I'm calling the following delegate:
lvwColumnSorter = New CustomLVSorter.CustomLVSorter()
lsv_OpenTickets.CustomSorter = Sub(column As OLVColumn, order As SortOrder)
lvwColumnSorter.ColumnToSort = Ticket_Status.Index
lvwColumnSorter._SortModifier = CustomLVSorter.CustomLVSorter.SortModifiers.SortByText
lvwColumnSorter.OrderOfSort = SortOrder.Ascending
lsv_OpenTickets.ListViewItemSorter = lvwColumnSorter
End Sub
I get no errors, but I also get no change.
Any help would be greatly appreciated.
Well, are you sure you have looked at every example? I think there are a lot of resources on this one.
When you're using a list, datagridview, or any main form, you can adjust it to use a custom sorter. You create a custom IComparer, i.e. the definition of how you sort something. It can be as simple as converting the string (like yours) to an int with CInt() and returning -1 or +1 if it is greater or less than the last value. This is very common.
If you need help on the basics of how to do it, of course there are always the microsoft links that give you the basics such as Custom Sort I Comparer. But there is a stack flow that also follow your problem here: Custom sort C#
It's in C#, but there are many converters on that around here.
But the easiest way to get around it? Convert your string list into a integer list. Then it will sort perfectly.
I'm just looking to be able to sort the results of a BatchedJoinBlock (http://msdn.microsoft.com/en-us/library/hh194683.aspx) so that the different results of the different targets stay together. I will explain! Example in some pseudo-code:
Dim batchedJoin = New BatchedJoinBlock(Of String, object)(4)
batchedJoin.Target1.Post("String1Target1")
batchedJoin.Target2.Post(CType(BuildIt, StringBuilder1))
batchedJoin.Target1.Post("String1Target2")
batchedJoin.Target2.Post(CType(BuildIt, StringBuilder2))
Dim results = batchedJoin.Receive()
'This sorts one result...
Dim SortByResult = results.Item1.OrderBy(Function(item) item.ToString, New NaturalStringComparer)
Basically I've got a string and an object, the SortByResult variable above sorts the strings exactly as I'd like them to sort. I'm looking for a way to get the objects that used to be at the same index number in target2 into the same order. e.g. if "String1Target1" changes order I'd like to somehow reliably refer to/pair it together with "StringBuilder1". The actual end result just needs to be that the objects (target2) are sorted in the order that is dictated by the strings being sorted (target1). Something like:
Dim EndResult = results.Item2.OrderBy(strings in target1)
but I'll gladly take an intermediate solution! I've also tried using a dictionary (results.Item2.ToDictionary) with the string as a key (which would also be a fine solution) but it's a bit beyond my ken using lamba expressions in the proper context. I can realistically do this in several steps with a list or something, but I'm trying to get something more efficient/learn something, and it seems like there's a lot of default options with the results of the jointblock that I'm just not experienced enough to use. Thanks in advance for any help you can provide!
To me, it looks like you don't actually want BatchedJoinBlock, because the two pieces of data always come together. A better option for that would be a BatchBlock of Tuple<string, object>. When you have that, you can then use LINQ directly to sort each batch:
results.OrderBy(Function(tuple) tuple.Item1)
I have a model:
class List:
data = ...
previous = models.ForeignKey('List', related_name='r1')
obj = models.ForeignKey('Obj', related_name='nodes')
This is one direction list containing reference to some obj of Obj class. I can reverse relation and get some list's all elements refering to obj by:
obj.nodes
But how Can I get the very last node? Without using raw sql, genering as little SQL queries by django as can.
obj.nodes is a RelatedManager, not a list. As with any manager, you can get the last queried element by
obj.nodes.all().reverse()[0]
This makes sense anyway only if there is any default order defined on the Node's Meta class, because otherwise the semantic of 'reverse' don't make any sense. If you don't have any specified order, set it explicitly:
obj.nodes.order_by('-pk')[0]
len(obj.nodes)-1
should give you the index of the last element (counting from 0) of your list
so something like
obj.nodes[len(obj.nodes)-1]
should give the last element of the list
i'm not sure it's good for your case, just give it a try :)
I see this question is quite old, but in newer versions of Django there are first() and last() methods on querysets now.
Well, you just can use [-1] index and it will return last element from the list. Maybe this question are close to yours:
Getting the last element of a list in Python
for further reading, Django does not support negative indexing and using something like
obj.nodes.all()[-1]
will raise an error.
in newer versions of Django you can use last() function on queryset to get the last item of your list.
obj.nodes.last()
another approach is to use len() function to get the index of last item of a list
obj.nodes[len(obj.nodes)-1]
Here is the single line from one of my functions to test if any objects in my array have a given property with a matching value
Return ((From tag In DataCache.Tags Where (tag.FldTag = strtagname) Select tag).Count = 1)
WHERE....
DataCache.Tags is an array of custom objects
strtagname = "brazil"
and brazil is definitely a tag name stored within one of the custom objects in the array.
However the function continually returns false.
Can someone confirm to me that the above should or should not work.
and if it wont work can someone tell me the best way to test if any of the objects in the array contain a property with a specific value.
I suppose in summary I am looking for the equivalent of a SQL EXISTS statement.
Many thanks in hope.
Your code is currently checking whether the count is exactly one.
The equivalent of EXISTS in LINQ is Any. You want something like:
Return DataCache.Tags.Any(Function(tag) tag.FldTag = strtagname)
(Miraculously it looks like that syntax may be about right... it looks like the docs examples...)
Many Thanks for the response.
Your code did not work. Then I realised that I was comparing to an array value so it would be case sensitive.
However glad I asked the question, as I found a better way than mine.
Many thanks again !