CareTaker22
CareTaker22

Reputation: 1300

Adding items from ComboBox to Datagrid

I need to insert items from my ComboBox to my Datagrid when I click on an item in my combobox(SelectionChanged event).

I load the data from a WCF application into the comboboxes with the following method:

private async Task LoadItems(TruckServiceClient TSC, QuoteOptionType type, ComboBox combobox)
{
    List<DisplayItems> displayItems = new List<DisplayItems>();
    foreach (var item in await TSC.GetQuoteOptionListAsync(type))
        displayItems.Add(new DisplayItems { Id = item.Key, Name = item.Value });
    combobox.ItemsSource = (displayItems.ToArray());
}

and then I sort which data goes where in my WindowLoaded event:

private async void QuoteWindow_Loaded(object sender, RoutedEventArgs e)
{
    using (TruckServiceClient TSC = new TruckServiceClient())
    {
        await LoadItems(TSC, QuoteOptionType.BodyType, cmbBodyType);
        await LoadItems(TSC, QuoteOptionType.Chassis, cmbChassisCab);
        await LoadItems(TSC, QuoteOptionType.PaintColor, cmbPaint);
        await LoadItems(TSC, QuoteOptionType.DropSide, cmbDropsideHeight);
        await LoadItems(TSC, QuoteOptionType.Floor, cmbFloor);
        await LoadItems(TSC, QuoteOptionType.RearDropSide, cmbRearDropsideHeight);
        await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras);
    }
}

What I am desperate to know is how would I go about to display the item that I just clicked on(in the combobox) to be added to the datagrid?

I have tried the following coding that I found on the internet, but I do not even know where to start to change the coding from what it is below to how I need it to work with my current way of loading the items into the comboboxes.

    Extras ex = (Extras)cmbAddExtras.SelectedItem;
    List<Extra> items = new List<Extra> { E };

    items.Where(item => item != null).ToList().ForEach(i =>
    {
        dgAddExtras.Items.Add(i);
    });

Upvotes: 1

Views: 87

Answers (1)

almulo
almulo

Reputation: 4978

Subscribe to the SelectiongChanged event, either on XAML or on your Loaded method. And then in the event handler simply retrieve the selected item and add it to the DataGrid:

private async void QuoteWindow_Loaded(object sender, RoutedEventArgs e)
{
    using (TruckServiceClient TSC = new TruckServiceClient())
    {
        await LoadItems(TSC, QuoteOptionType.BodyType, cmbBodyType);
        await LoadItems(TSC, QuoteOptionType.Chassis, cmbChassisCab);
        await LoadItems(TSC, QuoteOptionType.PaintColor, cmbPaint);
        await LoadItems(TSC, QuoteOptionType.DropSide, cmbDropsideHeight);
        await LoadItems(TSC, QuoteOptionType.Floor, cmbFloor);
        await LoadItems(TSC, QuoteOptionType.RearDropSide, cmbRearDropsideHeight);
        await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras);
    }

    combAddExtras.SelectionChanged += cmbAddExtras_SelectionChanged;
}

private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var item = cmbAddExtras.SelectedItem;

    if (item != null)
        dgAddExtras.Items.Add(item);
}

EDIT - If you want to remove the item from the Combo:

private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var item = cmbAddExtras.SelectedItem;

    if (item != null)
    {
        dgAddExtras.Items.Add(item);
        cmbAddExtras.Remove(item);
    }
}

If you don't wanna remove it from the Combo, but wanna check it isn't already in the DataGrid:

private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var item = cmbAddExtras.SelectedItem;

    if (item != null && !dgAddExtras.Items.Contains(item))
        dgAddExtras.Items.Add(item);
}

Upvotes: 2

Related Questions