I am using a combobox to add data to a datagrid with the coding below. What I want to achieve is accessing all of the 'item' properties/information that's connected to that selected item's id and then setting all of that information to a class (ExtraDisplayItems). How would I go about doing this?
var item = cmbAddExtras.SelectedItem;
if (item != null)
dgAddExtras.Items.Add(item);
Here is my class:
public class ExtraDisplayItems
{
public int ItemId { get; set; }
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
}
What I have gotten to work with databinding is displaying each of the item's 'id' in the datagrid and not any further.
EDIT: I am trying to use the same type that I used in my comboboxes, but I am having a bit of trouble.
I am just giving the extra information if it might help. Here is where I get all the information from my WCF service and then setting the information to my DisplayItems class in my WPF application:
private async Task LoadItems(TruckServiceClient TSC, QuoteOptionType type, ComboBox combobox)
{
List displayItems = new List();
foreach (var item in await TSC.GetQuoteOptionListAsync(type))
displayItems.Add(new DisplayItems { Id = item.Key, Name = item.Value });
combobox.ItemsSource = (displayItems.ToArray());
}
Then in the method below, I load the data from my LoadItems method into the specific comboboxes:
private async void QuoteWindow_Loaded(object sender, RoutedEventArgs e)
{
using (TruckServiceClient TSC = new TruckServiceClient())
{
await LoadItems(TSC, QuoteOptionType.BodyType, cmbBodyType);
...
await LoadItems(TSC, QuoteOptionType.RearDropSide, cmbRearDropsideHeight);
await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras); //Extras
}
//cmbAddExtras.SelectionChanged += cmbAddExtras_SelectionChanged;
}
In my final code snippet I am trying to set the cmbAddExtras.SelectedItem to my Type (which I think is: QuoteOptionType.Extras).
I am trying to set the SelectedItem as below, but with no clue how to do it.
var item = cmbAddExtras.SelectedItem as await QuoteOptionType.Extras;
or
var item = cmbAddExtras.SelectedItem as await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras);
I am getting the errors:
'await' cannot be used as an identifier within an async method or lamba expression &
; expected
Because this call is not awaited, execution of the current method continues before the call is complicated. Consider applying the 'await' operator to the result of the call.
As you can clearly see that I have NO clue what I am doing. Also here is my class where I create my QuoteOptionType class/enum. This is from my WCF application:
[DataContract]
[Flags]
public enum QuoteOptionType
{
[EnumMember]
BodyType,
[EnumMember]
Chassis,
[EnumMember]
PaintColor,
[EnumMember]
DropSide,
[EnumMember]
Floor,
[EnumMember]
RearDropSide,
[EnumMember]
Extras
}
2nd EDIT: Here I am using the Dictionary
public Dictionary GetQuoteOptionList(QuoteOptionType optionType)
{
Dictionary result = new Dictionary();
using (TruckDb db = new TruckDb())
{
switch (optionType)
{
case QuoteOptionType.BodyType:
db.BodyTypes.ToList().ForEach(x => result.Add(x.Id, x.Name));
break;
...
case QuoteOptionType.RearDropSide:
db.RearDropSides.ToList().ForEach(x => result.Add(x.Id, x.Name));
break;
case QuoteOptionType.Extras: // x.StockItem throws out the error: No overload for method 'Add' takes 3 arguments
db.Extras.ToList().ForEach(x => result.Add(x.Id, x.Description, x.StockItem));
break;
default:
throw new ArgumentException("The option that was selected does not have a corresponding list.");
}
}
return result;
}