Question

Here's my ComboBox:

<ComboBox HorizontalAlignment="Left"
          Margin="125,110,0,0"
          VerticalAlignment="Top"
          Width="120"
          DisplayMemberPath="lot_number"
          ItemsSource="{Binding LotNumList}"
          RenderTransformOrigin="0.583,2" Height="18" />

Here's a DataGrid that I want to update the values to:

<DataGrid HorizontalAlignment="Left" Margin="228,177,0,0" VerticalAlignment="Top" 
          Height="292" Width="617" ItemsSource="{Binding ComponentsList}" 
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Component" Binding="{Binding component}" CanUserResize="False"/>
        <DataGridTextColumn Header="Control" Binding="{Binding aControl}" CanUserResize="False"/>
        <DataGridTextColumn Header="Reference" Binding="{Binding cal_ref}" CanUserResize="False" />
        <DataGridTextColumn Header="Family" Binding="{Binding family}" CanUserResize="False"/>
        <DataGridTextColumn Header="Id" Binding="{Binding componentId }" CanUserResize="False"/>
    </DataGrid.Columns>

Here's how I'm grabbing data from db to populate the ComboBox:

//Grabs the lot_number column from db that is distinct
var lotNum = db.LotInformation.GroupBy(i => i.lot_number)
                              .Select(group => group.FirstOrDefault());

//Loops through the lot numbers column in db and converts to list 
foreach (var item in lotNum)
{
    Console.WriteLine(item.lot_number);
}
LotNumList = lotNum.ToList();

Now I am wondering how do I connect my ComboBox so that when I select a value in the ComboBox... then the DataGrid gets updated based on the value of the selection in the ComboBox.

I tried something like this:

private void UpdateExistLotList(string LotNumber)
{
    using (var db = new DDataContext())
    {
        //Grabs the lot_number column from db that is distinct
        var ExistLot = db.LotInformation.First(l => l.lot_number.Equals(LotNumber));
    }
}

Calling the method in my lot number list property but it doesn't get called or just doesn't work. I'm not sure what I'm doing wrong. Any ideas?

EDIT:

Properties:

public List<Components> ComponentsList
    {
        get 
        {
           return components;
        }
        set
        {
            components = value;
            RaisePropertyChanged("ComponentsList");
        }

    }

public string LotNumber
    {
        get
        {
            return lotNumber;
        }
        set
        {
            lotNumber = value;
            RaisePropertyChanged("LotNumber");
        }
    }

    public List<LotInformation> LotNumList
    {
        get
        {
            return lotNumList;
        }
        set
        {
            lotNumList = value;
            RaisePropertyChanged("LotNumList");
           UpdateExistLotList(LotNumber);

        }
    }

Here's where LotNumber is declared (I take the deserialized value of lot number from memory and assign it to LotNumber):

public void DeSerializationXML(string filePath)
    {
        XmlRootAttribute xRoot = new XmlRootAttribute();
        xRoot.ElementName = "lot_information";
        xRoot.IsNullable = false;

        // Create an instance of lotinformation class.
        var lot = new LotInformation();

        // Create an instance of stream writer.
        TextReader txtReader = new StreamReader(filePath);

        // Create and instance of XmlSerializer class.
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(LotInformation), xRoot);

        // DeSerialize from the StreamReader
        lot = (LotInformation)xmlSerializer.Deserialize(txtReader);

        // Close the stream reader
        txtReader.Close();

        //Storing deserialized strings to db
        using (var db = new DMIDataContext())
        {


            LotInformation newLot = new LotInformation();


            if (newLot != null)
            {
                newLot.Id = lot.Id;
                newLot.lot_number = lot.lot_number;
                newLot.exp_date = lot.exp_date;

                LotNumber = newLot.lot_number;
                ExpirationDate = newLot.exp_date.ToString();

                //Grabs the lot_number column from db that is distinct
                var lotNum = db.LotInformation.GroupBy(i => i.lot_number).Select(group => group.FirstOrDefault());

                //Loops through the lot numbers column in db and converts to list 
                foreach (var item in lotNum)
                {
                    Console.WriteLine(item.lot_number);
                }
                LotNumList = lotNum.ToList();

                foreach (Components comp in lot.Components)
                {
                    newLot.Components.Add(comp);

                }
                ComponentsList = newLot.Components;

                foreach (Families fam in lot.Families)
                {

                    newLot.Families.Add(fam);
                }
                FamiliesList = newLot.Families;

                try
                {
                    db.LotInformation.Add(newLot);
                    db.SaveChanges();
                    Console.WriteLine("successfully");
                }
                catch
                {
                    //TODO: Add a Dialog Here

                }
            }

        }
Was it helpful?

Solution

private void UpdateExistLotList()
{
    using (var db = new DDataContext())
    {
        //Grabs the lot_number column from db that is distinct
        var ExistLot = db.LotInformation.First(l => l.lot_number.Equals(LotNumber));
    }
}

This method has no para?

But you call it like this?

{
    lotNumList = value;
    RaisePropertyChanged("LotNumList");
    UpdateExistLotList(LotNumber);

}

Is there something wrong?

Your question is that UpdateExistLotList was never called?

Try to add a breakpoint on RaisePropertyChanged("LotNumList"); in VisualStudio and observe why it doesn't get called.

In your code, I don't know the use of ExistLot and LotNumber.

I guess your demand is like this?

Comobox shows LotInformation, select one LotInformation and make datagrid shows LotInformation.Components ?

If so,you can Binding DataGrid's Itemsource={Binding Components,ElementName=ComboboxName} or you can Binding Combobox's SelectedItem/SelectedValue, and then set ComponentsList in these events.


Get your demand.

You means you don't set a relationship between Components and LotInformation in EntityFramework or other DB framework. If you use EF, I advise you make a relationship between Components and LotInformation,and then you can get ComponentsList by LotInformation.Components.

In another way, try like this:

<ComboBox HorizontalAlignment="Left"
          Margin="125,110,0,0"
          VerticalAlignment="Top"
          Width="120"
          DisplayMemberPath="lot_number"
          SelectedItem="{Binding SelectedLot}"
          ItemsSource="{Binding LotNumList}"
          RenderTransformOrigin="0.583,2" Height="18" />


     private LotInformation selectedLot;

    public LotInformation SelectedLot
    {
        get { return selectedLot; }
        set
        {
            selectedLot = value;
            var lot = value as LotInformation;
            if (lot != null)
            {
                ComponentsList = new List<Components>();
                //add ComponentsList 
            }
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top