I have a ComboBox being bind to LINQ object as below.
Dim LearnTypeList = context.LearnTypes.OrderBy(Function(a) a.LearnType).ToList()
dlLearnedAbout.DataSource = LearnTypeList
dlLearnedAbout.DisplayMember = "LearnType"
dlLearnedAbout.ValueMember = "LearnType"
I am not able to use Index of Items to find Item with matching text as below .
MessageBox.Show(dlLearnedAbout.Items.IndexOf("Website"))
This always returns -1 even if its there inside the table and dropdown.Is it because the Item bound to the Dropdown is of type "LearnTypes ?
IndexOf("Website") is looking through the list for an item of type String. your list doesn't contain strings though - it contains objects of whatever type you have in context.LearnTypes. that's why you are getting -1 (aka item not found)
Related
enter image description hereScreenshot of error I am accessing the value from a structure in tableviewcell class , I wanted the value from the structure for making the api call .
Your error is
Can not subscript [Section] with index of type (item).type
is for this line
let item: Item = sectionData[item]
It explains that you cannot use item type value while subscripting on array.
You will need to pass some Index, i.e some Int value
Update your code with some proper index
let item: Item = sectionData[indexOfSectedRow]
Hope it helps.
I'm currently trying to implement AJAX results filtering on a certain page.
I created the dropdowns(on the client side), so that they have the umbraco prevalue id as their value.
I will then send this id to the server, rather than the text value. Then I loop through my content to find items with this same id.
The problem, however, is that I can't figure out how to get the value id from the property. Everything either returns the text value, or just a 0 value.
This is being performed in an ApiController.
These are all of the options I've tried:
IPublishedContent root = Umbraco.TypedContentAtRoot().First();
var downloads = root.Children.Where(q => q.Name == "Downloads").SingleOrDefault();
foreach (var item in downloads.Children)
{
var test = item.GetPropertyValue<int>("classification");
var testing = item.GetProperty("classification");
var testVal = testing.DataValue;
var testValToo = testing.GetValue<int>();
var testThree = testing.Value;
}
These are the results in order:
- 0
- IPublishedProperty
- "textValue"
- 0
- "textValue"
Is it possible to get the selected value id from a dropdownlist property? Or is string matching my only option to compare values?
EDIT:
Nevermind, found the solution. Posting the answer here, in case someone else needs it.
I was using the data type dropdownlist. I should have been using dropdownlist:publishing keys.
dropdownlist only ever returns a value. dropdownlist:publishing keys, however, returns the prevalue id, rather than the text value.
Source
Something like this perhaps.
library.GetPreValueAsString(node.GetProperty<int>("sectionType")).ToLower()
I have a DataTable I would like to filter based on elements in a Dictionary.
The Dictionary key is an integer and the value portion is a class.
The contained class has a field called ItemId. This ItemId is a field in the datatable.
I have a value for the key portion of the dictionary.
What I would like is a LINQ query which returns an Enumerable subset of the datatable based on the value I have for the key.
In other words I want a result set of all records in the datatable whose ItemId column is in records contained in the dictionary for which I have the Key value.
Is this possible?
If I understand, you have a key. So with the key, you have one value of the dictionary. This seems very simple to obtain what you want :
int key = 3;
var enumerable = dataTable.Where(t => t.ItemId == dictionary[key].ItemId);
I am using Northwind Customers Table where I fill the dataset and get the datatable.
I am trying to use dynamic linq and want to select columnName dynamically
var qry = MyDataTable.AsEnumerable().AsQueryable().Select("new(Country)");
Right now I have hard coded country but even then I get this error
No property or field 'Country' exists in type 'datarow'
I would like to eventually change this query to take the column name dynamically.
Please help!!! thanks.
The important hint is here (in bold):
No property or field 'Country' exists
in type 'datarow'
The extension method AsEnumerable of the DataTable class returns an IEnumerable<T> where T has the type DataRow. Now the Select method of Dynamic LINQ wants to work with this type DataRow which hasn't a property Country of course.
You could try this instead:
var qry = MyDataTable.AsEnumerable().AsQueryable()
.Select("new(it[\"Country\"] as CountryAlias)");
it now represents a variable of type DataRow and you can use methods of this type and perhaps also the indexer in my example above. (Dynamic LINQ supports accessing array elements by an integer index, but I am not sure though if accessing an indexer with a string key will work.)
I've used Slauma's answer and it worked. In addition i was doing OrderBy with dynamic linq maybe this will help to someone. I'll just drop the code here.
string dynamicLinqText = $"it[\"{sortColumnName}\"] {sortDirection}"; //it["PERSON_NAME"] asc
result = result.AsEnumerable().OrderBy(dynamicLinqText).CopyToDataTable();
The data sources have the same structure, but different data. One would be used for rows that are saved (view mode), and the other one would be for rows that are being added or edited (edit/new rows). How can that be acomplished?
I have a standard foreign key column that references a standard lookup table which has an ID, Name and Active (bit). The combo box column uses that lookup table to show the list, but only active items. Let's say a lookup item is used and later deactivated (Active = 0). The combo box column now shows errors because the ID is not found in the list. Does anyone have any ideas how to solve it?
Not sure if you're still looking but I've just come across the same problem. Here's how I addressed it, hope it is of use to you.
Define a function which returns a DataGridViewComboCell with the appropriate datasource set (collection objects defined elsewhere, note that I'm using EntitySpaces in this example to populate the DataSource):
Private Function GetStockComboDataSource(ByVal type As eStockComboType) As DataGridViewComboBoxCell
Try
Dim cell As New DataGridViewComboBoxCell
Select Case type
Case eStockComboType.StockIn
cell.DataSource = Me.StockInCol.Query.LoadDataTable
cell.DisplayMember = "FullName"
cell.ValueMember = JCStockInMetadata.ColumnNames.StockItemID
Case eStockComboType.StockItem
cell.DataSource = StockItemCol.Query.LoadDataTable
cell.ValueMember = JCStockItemMetadata.ColumnNames.StockItemID
cell.DisplayMember = "FullName"
End Select
Return cell
End Function
Now when it comes to setting the combo DataSource (I use the RowEnter event here for example):
Private Sub dgvStock_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvStock.RowEnter
Try
If IsNumeric(Me.dgvStock.Rows(e.RowIndex).Cells("ID").Value) Then
Dim intValue As Integer = Convert.ToInt32(Me.dgvStock.Rows(e.RowIndex).Cells("Stock Item").Value)
Me.dgvStock.Rows(e.RowIndex).Cells("Stock Item") = GetStockComboDataSource(eStockComboType.StockItem)
Me.dgvStock.Rows(e.RowIndex).Cells("Stock Item").Value = intValue
Else
Me.dgvStock.Rows(e.RowIndex).Cells("Stock Item") = GetStockComboDataSource(eStockComboType.StockIn)
End If
Catch ex As Exception
JCExceptionLogger.LogException(ex)
End Try
End Sub
Here I switch the combo DataSource depending on whether the ID column is numeric but you can implement whatever business logic you require. Note that I save the value of the cell before setting the combo (intValue). This appears to be necessary otherwise the combo will display as blank.