2016-02-01 96 views
0

我有2个网格一个是国家和另一个是程序。现在我想将两个国家或者一个或者多个程序组合成第三个DataGrid。
数据绑定到dgProgramme [GridView控件]如下循环通过每个项目WPF DataGrid

using (DataSet dataset = DBHandler.GetProgramme()) 
{ 
    dgProgramme.ItemsSource = dataset.Tables[0].DefaultView; 
} 

数据绑定到国家[DataGrid中]如下

using (DataSet ds = DBHandler.GetCountries()) 
{ 
    dgCountries.ItemsSource = ds.Tables[0].DefaultView; 
} 

现在我想选定值添加到名为gRolesForUser 目前代码三的DataGrid将选定值添加到新的[gRolesForUser]网格中。但是它的不检查,这就是为什么我打算请检查复制如果sleeted组合的存在比不添加到数据网格[gRolesForUser]

var itemsSource = dgRolesForUser.ItemsSource as IEnumerable; 
if (itemsSource != null) 
{ 
    foreach (var drv in itemsSource) 
    { 
     // Here I want to check that SelectedItems not Exists into using something like follows 
     if(PersonId != [dgRolesForUser].CountryId && CountryName !=[dgRolesForUser].CountryName) 
     //then add to list 
     _user.Roles.Add(
     new PersonRole() 
     { 
      PersonId = _user.PersonId, 
      Amount = string.IsNullOrWhiteSpace(txtDefaultAmount.Text) ? null : (Decimal?)Convert.ToDecimal(txtDefaultAmount.Text), 
      CountryId = Convert.ToInt32(((DataRowView)dgCountries.SelectedItems[i])["CountryId"]), 
      RoleId = Convert.ToInt32(cmbRole.SelectedValue.ToString()), 
      CountryName = (((DataRowView)dgCountries.SelectedItems[i])["Name"]).ToString(), 
      ProgrammeName = (((DataRowView)dgProgramme.SelectedItems[j])["Name"]).ToString(), 
      RoleName = cmbRole.Text.ToString() 
     } 
    ); 
    } 
} 

回答

0

使用LINQ

var itemsSource = dgRolesForUser.ItemsSource as IEnumerable<PersonRole>; 
if(itemsSource.All(item => item.CountryId != PersonId && item.CountryName != CountryName) 
{ 
    // Combination does not exist, add new role here 
} 

(猜你ItemsSourceIEnumerable<PersonRole>

+0

出现以下错误 严重程度\t代码\t描述\t项目\t文件\t行\t抑制状态 错误\t CS1061 \t'IEnumerable'不包含'All'的定义,并且没有找到接受'IEnumerable'类型的第一个参数的扩展方法'All'(您是否缺少using指令或) – user3624511

+0

@ user3624511'using System.Linq;'应该解决这个问题 –

+0

我已经使用System.Linq; – user3624511