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

3

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

以下是我如何从数据库中获取数据来填充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中选择一个值时...基于ComboBox中的选择值更新DataGrid。
我尝试了以下内容:
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

                }
            }

        }

@Grant Winney,更新了帖子。我在我的List<LotInformation> LotNumList属性中调用了UpdateExistLostList。LotNumber是反序列化并存储在内存中的批次号。如果帖子有点乱,请原谅,我一直在更改参数,可能会有垃圾代码:/ 如果您有任何问题,请告诉我。谢谢。 - Kala J
@Rang,你好,我会编辑我的帖子,然后你们就可以看到我的混乱代码。如果你有任何进一步的问题,请告诉我。对于50声望的事情,我感到很抱歉。 - Kala J
1个回答

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

这个方法没有参数?

但你要这样调用它?

{
    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或其他DB框架中没有设置Components和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 
            }
        }
    }

实际上很抱歉,UpdateExistLotList 应该有字符串型的LotNumber作为参数。因此,应为 UpdateExistLotList(string LotNumber)。在调试器中,LotNumber参数可以正常工作。只是组合框和数据网格之间的绑定不起作用。 - Kala J
ComboBox只显示与LotInformation表相关联的lot_number列。问题是,我想从其他具有外键到批号表主键的表中提取数据。 - Kala J
另外,如果我上面所做的事情太令人困惑,那没关系。您能否提供一个简单的示例,说明如何在选择ComboBox中的项目后更新DataGrid?我可以跟着一个简单的示例。再次感谢! - Kala J
我将对其进行测试,顺便说一下,我在foreach循环中遇到了一个“The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.”的异常。我猜我只需要设置我的db context?我会查看GitHub的,感谢你的帮助! :D - Kala J
是的,这是错误的。因为在foreach中添加/删除会改变源代码。我只是提供了一种方法。您可以按自己的方式设置ComponentsList。 - Rang
谢谢!我相信我已经把它搞定了...至少现在是这样的:D 顺便说一下,如果你想回答的话,我还有另一个问题,http://stackoverflow.com/questions/23434823/wpf-how-do-i-validate-that-a-number-from-memory-is-not-duplicated-in-db-table - Kala J

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接