当用户在ComboBox中进行选择时,如何更新DataGrid? [英] How can I update my DataGrid when a user makes a selection in the ComboBox?

查看:250
本文介绍了当用户在ComboBox中进行选择时,如何更新DataGrid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的ComboBox:

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" />

这里是一个DataGrid,我想更新的值:

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>

以下是我从db中获取数据以填充ComboBox的方法:

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



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

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.

我试过这样:

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?

编辑:

属性:

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

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

                }
            }

        }


推荐答案

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 从未被呼叫过?

Your question is that UpdateExistLotList was never called?

尝试添加断点

在您的Visual Studio中,您可以在 RaisePropertyChanged(LotNumList); 代码,我不知道使用 ExistLot LotNumber

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

我想你的需求是这样的?

I guess your demand is like this?

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

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

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

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.

获取您的需求。

这意味着您不要在EntityFramework或其他DB框架中设置Components和LotInformation之间的关系。
如果你使用EF,我建议你在Components和LotInformation之间建立关系,然后你可以通过 LotInformation.Components 获得ComponentsList。

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.

换句话说,尝试这样:

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

这篇关于当用户在ComboBox中进行选择时,如何更新DataGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆