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

标签 c# wpf datagrid 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>

下面是我如何从数据库中获取数据来填充 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 中的选择值进行更新。

我试过这样的:

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

                }
            }

        }

最佳答案

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 ?

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


获取您的需求。

您的意思是您没有在 EntityFramework 或其他数据库框架中设置 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 
            }
        }
    }

关于c# - 当用户在 ComboBox 中进行选择时,如何更新我的 DataGrid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23531386/

相关文章:

c# - 什么是数字串

c# - 如何在 Magick.NET Read 中设置位深度

c# - 具有 AutoGenerateColumns 的 WPF Datagrid 中的 ItemTemplateSelector

c# - 如何在未指定路径的代码隐藏中创建绑定(bind)?

javascript - 删除DataGrid(Dojo)实例和表(DataGrid)正确显示

silverlight - 使 Silverlight Datagrid 单元格不可选择

c# - 为什么我的角色/相机在 Unity 2d 中卡顿?

c# - 我无法使用 wpf 设置 Web 文本框值

c# - WPF:如何通过 XAML 将整个控件作为 CommandParameter 传递?

c# - WPF 数据网格中的错误