【问题标题】:DataGrid SelectedItem to TextBoxDataGrid SelectedItem 到 TextBox
【发布时间】:2018-06-06 06:31:52
【问题描述】:

我有这个代码来填充我的DataGrid

private void getData()
{
    var context = new dbStudentEntities();        
    var data = (from s in context.tblStudents
                join c in context.tblClasses
                on s.classID equals c.classID
                select new {
                        s.studentID, c.classID, s.firstName, s.middleName, 
                        s.lastName, s.age, c.className});

    dgStudents.ItemsSource = data.ToList();
}

现在我希望我的 TextBox 用 DataGrid 中的 SelectedItem 填充。我的 SelectionChanged 事件中有这段代码:

dbStudentEntities context = new dbStudentEntities();
try
{
    txtFirst.Text = ((tblStudent)dgStudents.SelectedItem).firstName.ToString();
    txtMiddle.Text = ((tblStudent)dgStudents.SelectedItem).middleName.ToString();
    txtLast.Text = ((tblStudent)dgStudents.SelectedItem).lastName.ToString();
    txtAge.Text = ((tblStudent)dgStudents.SelectedItem).age.ToString();
}
catch (Exception ex)
{
    MessageBox.Show("" + ex);
}

但是每次选择一行,都会弹出这个错误:

无法转换类型的对象 'f__AnonymousType07[System.Int32,System.Int32,System.String,System.String,System.String,System.Nullable1[System.Int32],System.String]' 输入“EFStudents.tblStudent”。

我该怎么办?

【问题讨论】:

  • 您没有将 tblStudent 对象添加到列表中。我不确定您是否可以在 winforms 中执行此操作,但在 wpf 中您只需将 tblStudent 对象列表作为 ItemsSource 添加到 listView。 new { } 是一个匿名类,所以这个错误是有道理的
  • 我使用 WPF 而我没有使用 listview。
  • 那么就完全没有问题了。只要看看我的回答,如果你的 DataGrid 上有 AutoGenerateColumns="True" ,你应该很高兴
  • 很抱歉问这位先生,但是如何?我是 wpf 的新手。
  • 我认为错误在这里:((**tblStudent**)dgStudents.SelectedItem).firstName.ToString();。因为我加入了 linq 查询来填充我的 DataGrid,但我只从 tblStudent 获取 SelectedItem。我不知道用什么来代替它。

标签: c# wpf entity-framework


【解决方案1】:

好的,首先,我假设您使用 EntityFramework 并且您的 Student 类看起来像这样:

public class Student
{
   public int Id {get;set;}
   public string Name {get;set;} // and other names and age and so on
   public ICollection<Class> Classes {get;set;} // classes to which student attends
}

// your context

public class dbStudentEntities : DbContext
{
   public DbSet<Student> tblStudents {get;set;} // in CodeFirst this is enough to also create Classes table
}

//    then to populate DataGrid:

using(var ctx = new dbStudentEntities()) // this is your context, disposing it is important
{
   var students = ctx.Students.Include(x => x.Classes).ToList(); // Student.Classes is not virtual, so we don't have lazy loading, but making it virtual creates ugly proxy. You can read up on that
   dataGrid.ItemsSource = students;
}

您不需要那种丑陋的 LINQ - EF 可以为您完成这项工作。

现在 DataGrid.SelectedItem 将是 Student 类型,因此您可以使用您的投射。

【讨论】:

    【解决方案2】:

    在您的 .xaml 文件中:

    <DataGrid Name="dgStudents" AutoGenerateColumns="True">
    </DataGrid>
    

    您可以尝试创建对象列表:

    List<tblStudent> data = context.tblStudents.ToList();
    dgStudent.ItemsSource = data;
    

    我可能犯了一些语法错误,所以看看编译器错误是什么,如果它不起作用。

    首先确保数据在数据网格中,然后您可以担心选择项目时会发生什么。

    【讨论】:

    • 无法将类型“EFStudents.dbStudentEntities”隐式转换为“System.Collections.Generic.List”EFStudents
    • 这意味着您的上下文不是 tblStudents 列表。然后在我的答案中尝试 var
    • 无法将类型“EFStudents.dbStudentEntities”隐式转换为“System.Collections.IEnumerable”。存在显式转换(您是否缺少演员表?)
    • 你能从 EFStudents.dbStudentEntities 中至少选择一个对象吗?如果不清楚这是什么类型的 objectcollection,你什么也做不了? EFStudents.dbStudentEntities 的类型是什么?
    • System.NotSupportedException: '无法在 LINQ to Entities 查询中构造实体或复杂类型'dbStudentModel.tblStudent'。'
    猜你喜欢
    • 2019-12-05
    • 2013-01-20
    • 2014-10-10
    • 2012-02-12
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2011-02-09
    相关资源
    最近更新 更多