Search code examples
c#linqwpfdatagrid

Getting information from a datagrid


I have a datagrid filled with information from a local database. When I click on one of these options I want put that information into a variable and put the information into a textblock.

I have some code fro this however the 'selected' always returns a null.

Here is the code:

private void lbxManagerDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ManagerTBL selected = lbxManagerDisplay.SelectedItem as ManagerTBL;

    if (selected != null)
    {
        txtblock_ManagerName.Text = selected.Id.ToString();
    }
}

And here is how I populated the datagrid

 var PopulateManagers = from m in db.ManagerTBLs
                   where m.ManagerName != null
                   orderby m.TeamName descending
                   select new
                   {
                       ID = m.Id,
                       Manager_Name = m.ManagerName,
                       Nationality = m.ManagerNationality,
                       Team = m.TeamName,
                       Trophies = m.TrophyCount,
                   };
lbxManagerDisplay.ItemsSource = PopulateManagers.ToList();

Just so everyone knows lbxManagerDisplay is a DataGrid


Solution

  • Your problem is that you don't select objects of type ManagerTBL but you select anonymous objects from you data base with this line:

    select new {
    

    So it is not possible for the compiler to cast it in the SelectionChanged event.

    Try and change the select statement to (I don't know the structure of the class ManagerTBL so I suggest to take the entire entry):

    var PopulateManagers = from m in db.ManagerTBLs
    
               where m.ManagerName != null
               orderby m.TeamName descending
               select m;
    

    You can also take only a piece of information, but if you want to cast it to a specific type you need to make a custom class to store this information

    public class MTBL_DataContainer
    {
        public int ID { get; set; }
        public string Manager_Name { get; set; }
        public string Nationality  { get; set; }
        public string Team { get; set; }
        public int Trophies { get; set; }    
    }
    
    
    var PopulateManagers = from m in db.ManagerTBLs
                   where m.ManagerName != null
                   orderby m.TeamName descending
                   select new MTBL_DataContainer
                   {
                       ID = m.Id,
                       Manager_Name = m.ManagerName,
                       Nationality = m.ManagerNationality,
                       Team = m.TeamName,
                       Trophies = m.TrophyCount,
                   };
    lbxManagerDisplay.ItemsSource = PopulateManagers.ToList();
    

    And in the end you can cast it like this:

    private void lbxManagerDisplay_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MTBL_DataContainer selected = lbxManagerDisplay.SelectedItem as MTBL_DataContainer;
    
        if (selected != null)
        {
            txtblock_ManagerName.Text = selected.ID.ToString();
        }
    }