BindingSource column with anonymous class as DataSource

Is there any method for selecting the current row column of the BindingSource whose DataSource is anonymous class?

var userResults = from u in dataContext.Users
          join c in dataContext.Computers on u.ID equals c.UserID
          where doSearch
             && u.Name.Contains(userNameTextBox.Text)
             && u.UserName.Contains(userUsernameTextBox.Text)
          select new { u.Name, u.UserName, u.Branch, c.Installations, u.ID };
userBindingSource.DataSource = userResults;

      

I want to get the current value u.ID

.

+2


source to share


1 answer


The problem here is that the elements in the BindingSource are of type Object and you need to convert them back to their anonymous type and you cannot do that ... in fact, you can (check the Casting to Anonymous Types section). but this is a clever trick and may not work in the future.

Other (better) parameters to get the property value:



  • Use a concrete type instead of an anonymous type
  • Use reflection to get the value of a property
  • C # 4.0 only: use dynamic keyword then call required property
  • Get value in bound control (for example, if your datasource is bound to DataGridView, get datagridview cell value instead of binding source value)
+2


source







All Articles