从DataGrid获取单元格值

时间:2018-07-10 14:16:14

标签: c# sql wpf linq

我有一个DataGrid,其中填充了来自联接的数据。

    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;
    }

我想从选定的行中获取数据并将其放置在不同的文本框中。经过大量令人沮丧的谷歌搜索并尝试最终找到一种可行的解决方案:

    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();
    }

问题在于,这是一种相当复杂的方法,可以执行不需要那么困难的事情。我希望为更简单的代码提供建议。

2 个答案:

答案 0 :(得分:1)

获取所选项目的属性非常复杂,因为所选项目具有匿名类型。

创建类型

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

在填充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
            };

,您将能够将SelectedItem转换为具体类型并读取属性而无需进行反射:

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;
}

如果您仍然想继续使用匿名类型,则可以使用dynamic类型来读取值(仅ShowSelectedTeam方法更改):

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();
}

答案 1 :(得分:1)

创建一个包含名称beliggenhet,arena和Division属性的类型:

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

ItemsSource设置为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
                };

并将SelectedItem属性强制转换为您的类型:

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