Quantcast
Viewing all articles
Browse latest Browse all 18858

WPF Datagrid Reset SelectedItem

Hi,

i am using a datagrid to edit an observablecollection of persons (binding to Itemssource). I also have a dictionary of persons (same type). So when i write the lastname in a cell i want that after
edit ends (on enter) the software searches in the dictionary for an existing entry and replaces the Person in my collection. That means that the object in the observablelist should be completely replaced by the dictionary object. Therefore i wrote following code:

private void dgPersons_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
  if (e.Column.DisplayIndex == 1) //Name Column
  {
    string name = ((TextBox)e.EditingElement).Text.ToLower();
    var dict = App.Dict.Where(p=> p.Name.ToLower() == name.ToLower()).ToList();
    if (dict.Count > 0)
      dgPersons.SelectedItem = dict[0];
    else
    {
      MessageBox.Show("Cannot find Name: " + name, "Not Found");
      e.Cancel = true;
      ((TextBox)e.EditingElement).Text = "";
    }
  }
}

If i do it like that nothing happens, if i add the dictionary person to my observablelist the Person appears in the datagrid but also the entered before. What is the best way to handle such a situation. I guess it should be a common way to edit items?

Kind Regards Manu


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>