您好我正在使用wpf datagrid。我有dataGrid_Division包含3列D_ID,D_Name和Branch_ID。

还有另一个datagrid dataGrid_Branch包含2列B_ID和B_Name
我想在dataGrid_Branch中获取用户选择的记录,它存储B_ID,并在我的数据库Division.B_ID中查找具有相同ID的所有结果,这只是一对多的关系。

然后我希望所有结果显示在dataGrid_Division上,所有记录具有相同的B_ID ID。
所以总结dataGrid.Branch上所选行的Select * from Division where ID = ID抱歉,我的英语不好。

private void Departments_page_Loaded(object sender, RoutedEventArgs e)
        {
            _dbObj = new medical_databaseEntitiescon();
            dataGrid_Branch.ItemsSource = _dbObj.branches.ToList();
        }

        private void dataGrid_Branch_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            _dbObj = new medical_databaseEntitiescon();

           // I want to Do the action here

        }

最佳答案

您的代码应如下所示:

var selectedBranch = dataGrid_Branch.SelectedItem as Branch;
var selectedDivisions = _dbObj.divisions.Where(x => x.B_ID == selectedBranch.B_ID).ToList();

dataGrid_Division.ItemsSource = selectedDivisions;


您没有提及部门结构的名称,因此我认为它是divisions

关于c# - 在Datagrid WPF的列上获取选定项,并由Linq查找,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44512221/

10-11 14:30