PellePetimeter
PellePetimeter

Reputation: 33

Get cell values from DataGrid

I have a DataGrid what is populated with data from a join.

private void GetTeamData()
{
    DataContext dc = new DataContext(connString);
    Table<Team> tblLag = dc.GetTable<Team>();
    Table<Division> tblDivision = dc.GetTable<Division>();

    var teams = from team in tblLag
                join division in tblDivision on team.Division equals division.Id
                select new
                {
                    name = team.TeamName,
                    beliggenhet = team.Location,
                    arena = team.Arena,
                    division = division.Name
                };

    dgTeams.ItemsSource = teams;
}

I want to get the data from the selected row and place it in different textboxes. After a lot of frustrating googling and trying i finaly find a solution what works:

private void ShowSelectedTeam(Object sender, RoutedEventArgs args)
{
    GetDivisionNames();
    dgTeams.SelectionUnit = DataGridSelectionUnit.FullRow;
    Object selectedTeam = dgTeams.SelectedItem;

    List<PropertyInfo> props = new List<PropertyInfo>(selectedTeam.GetType().GetProperties());

    tbxTeam.Text = props[0].GetValue(selectedTeam, null).ToString();
    tbxBeliggenhet.Text = props[1].GetValue(selectedTeam, null).ToString();
    tbxArena.Text = props[2].GetValue(selectedTeam, null).ToString();
    cbxDivisions.Text = props[3].GetValue(selectedTeam, null).ToString();
}

The problem is that this is a pretty complicated way to do something what shouldn't need to be that difficult. I would be glad for suggestions for a more simplistic code.

Upvotes: 0

Views: 141

Answers (2)

ASh
ASh

Reputation: 35646

it is so complicated to get selected item properties, because selected item has anonymous type.

create a type

public class TeamViewModel
{
    public string name { get; set; }
    public string beliggenhet { get; set; }
    public string arena { get; set; }
    public string division { get; set; }
}

create objects of that type when filling ItemsSource:

var teams = from team in tblLag
            join division in tblDivision on team.Division equals division.Id
            select new TeamViewModel
            {
                name = team.TeamName,
                beliggenhet = team.Location,
                arena = team.Arena,
                division = division.Name
            };

and you will be able to cast SelectedItem to concrete type and read properties without reflection:

private void ShowSelectedTeam(Object sender, RoutedEventArgs args)
{
    GetDivisionNames();
    dgTeams.SelectionUnit = DataGridSelectionUnit.FullRow;
    var selectedTeam = dgTeams.SelectedItem as TeamViewModel;

    if (selectedTeam == null) return;

    tbxTeam.Text = selectedTeam.name;
    tbxBeliggenhet.Text = selectedTeam.beliggenhet;
    tbxArena.Text = selectedTeam.arena;
    cbxDivisions.Text = selectedTeam.division;
}

in case you still want to continue using anonymous types, you can use dynamic type to read values (only ShowSelectedTeam method changes):

private void ShowSelectedTeam(Object sender, RoutedEventArgs args)
{
    GetDivisionNames();
    dgTeams.SelectionUnit = DataGridSelectionUnit.FullRow;
    dynamic selectedTeam = dgTeams.SelectedItem;

    if (selectedTeam == null) return;

    tbxTeam.Text = selectedTeam.name.ToString();
    tbxBeliggenhet.Text = selectedTeam.beliggenhet.ToString();
    tbxArena.Text = selectedTeam.arena.ToString();
    cbxDivisions.Text = selectedTeam.division.ToString();
}

Upvotes: 1

mm8
mm8

Reputation: 169190

Create a type that holds the name, beliggenhet , arena and division properties:

public class YourType
{
    public string Name { get; set; }
    public string Beliggenhet { get; set; }
    public string Arena { get; set; }
    public string Division { get; set; }
}

Set the ItemsSource to an IEnumerable<YourType>:

var teams = from team in tblLag
                join division in tblDivision on team.Division equals division.Id
                select new YourType
                {
                    Name = team.TeamName,
                    Beliggenhet = team.Location,
                    Arena = team.Arena,
                    Division = division.Name
                };

And cast the SelectedItem property to your type:

YourType selectedTeam = dgTeams.SelectedItem as YourType;
if (selectedTeam != null)
{
    tbxTeam.Text = selectedTeam.Name;
    tbxBeliggenhet.Text = selectedTeam.Beliggenhet;
    ...
}

Upvotes: 1

Related Questions