开发者

Edit wpf datagrid row after filtering

I have a WPF C# datagrid which I can filter through wit开发者_开发知识库h an SqlDataAdapter and display it with the ItemsSource property.

I can also update/delete rows before filtering but not after.

diamedbEntities objContext;
Sender objSendToEdit;

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    objContext = new diamedbEntities();
    dgEmp.ItemsSource = objContext.Senders;
}

private void dgEmp_SelectionChanged(object sender, SelectionChangedEventArgs e)
{       
    objSendToEdit = dgEmp.SelectedItem as Sender;
}

private void btnDelete_Click(object sender, RoutedEventArgs e)
{
    if (objSendToEdit == null)
    {
        MessageBox.Show("Cannot delete the blank Entry");
    }
    else
    {
        objContext.DeleteObject(objSendToEdit);
        objContext.SaveChanges();
        MessageBox.Show("Record Deleted..");
    }
}

After filtering objSendToEdit is null. How can I solve this?


http://msdn.microsoft.com/en-us/library/cscsdfbt(v=VS.100).aspx

The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception.

What are you trying to do with objSendToEdit = dgEmp.SelectedItem as Sender;?


This is because filter must be clearing the selection thus making objSendToEdit null.

You should re-select the objSendToEdit back onto the data grid immediately after filtering.

  objSendToEdit = dgEmp.SelectedItem;
  //// filter code
  dgEmp.SelectedItem = objSendToEdit;

Let me know if this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜