当用户在ComboBox中进行选择时,如何更新我的DataGrid?

时间:2014-05-08 01:49:43

标签: c# wpf datagrid combobox

这是我的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" />

这是我要将值更新为:

的DataGrid
<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>

以下是我如何从db中获取数据以填充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();

现在我想知道如何连接我的ComboBox,这样当我在ComboBox中选择一个值时......然后DataBrid会根据ComboBox中的选择值进行更新。

我试过这样的事情:

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));
    }
}

在我的批号列表属性中调用该方法,但它没有被调用或只是不起作用。我不确定我做错了什么。有什么想法吗?

编辑:

属性:

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);

        }
    }

这里是声明LotNumber的地方(我从内存中取出批号的反序列化值并将其分配给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

                }
            }

        }

1 个答案:

答案 0 :(得分:2)

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));
    }
}

这种方法没有para?

但你这样称呼它?

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

}

有什么不对吗?

您的问题是UpdateExistLotList从未被调用过?

尝试在VisualStudio中RaisePropertyChanged("LotNumList");添加一个断点,并观察它为什么不被调用。

在您的代码中,我不知道ExistLotLotNumber的使用情况。

我猜你的需求是这样的吗?

Comobox显示LotInformation,选择一个LotInformation并使datagrid显示LotInformation.Components?

如果是这样,您可以绑定DataGrid的Itemsource={Binding Components,ElementName=ComboboxName} 或者你可以绑定Combobox的SelectedItem / SelectedValue,然后在这些事件中设置ComponentsList。


满足您的需求。

您的意思是您没有在EntityFramework或其他数据库框架中设置组件和LotInformation之间的关系。 如果您使用EF,我建议您在Components和LotInformation之间建立关系,然后您可以按LotInformation.Components获取ComponentsList。

另一方面,请尝试这样:

<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 
            }
        }
    }