如果已经列出,如何比较两个wpf数据网格值中的值?

时间:2015-03-11 05:27:43

标签: c# wpf datagrid datarow datarowview

我试过以下代码来检查sid值,有两个datagrid,

在一个数据网格中,我选择一行并使用此处提到的tabindex发送到另一个datagrid2。

第一次发送意味着,它想要添加到datagrid2.Then如果再次从datagrid1和send手段中选择相同的数据行,它想要显示"它已经在datagrid2"中列出了 - 这是我的概念

但是当我尝试下面的代码时,它总是在datagrid3中列出,那么如何为我的概念完美地改变代码???

public void sendstudy(string sid, DataRow row)       
{ 

  this.datatableobject.Rows.Add(row);
  dataGrid2.ItemsSource = this.datatableobject.DefaultView;
  tabControl1.SelectedIndex = 1;


  foreach (DataRowView rowww in dataGrid2.Items)
  {
    string text = rowww.Row.ItemArray[1].ToString();
    if (text == sid)
    System.Windows.Forms.MessageBox.Show("ALREADY");
    return;
  }
}

2 个答案:

答案 0 :(得分:1)

List<string> results = new List<string>();
if (results.Contains(sid))
{
  System.Windows.Forms.MessageBox.Show("Study Already Listed");
  return;
}

它必须正常工作!!!

答案 1 :(得分:0)

阅读评论

public void sendstudy(string sid, DataRow row)       
{ 

  // row added to TableObject
  this.datatableobject.Rows.Add(row);

  // Row added to dataGrid2 - as its the same obj we added row to in last step.
  dataGrid2.ItemsSource = this.datatableobject.DefaultView;
  tabControl1.SelectedIndex = 1;

  // I don't know how many rows are there, or may be you want to iterate on datatableobject instead ?
  foreach (DataRowView rowww in dataGrid2.Items)
  {
    // text will be different for all other rows but the one you added
    string text = rowww.Row.ItemArray[1].ToString();
    // if would set for your row if text mathches
    if (text == sid)
    {
        System.Windows.Forms.MessageBox.Show("ALREADY");
        return;
    }
  }
}

现在如果任何rowww.Row.ItemArray [1]是sid,它会显示msgBox,否则它不会