从DataGridView中检索选定的行(类)作为对象类(无法转换对象)

时间:2017-06-05 08:39:41

标签: c# .net winforms datagridview

我有以下名为Students的课程:

namespace ClassLibrary
{
    using System;
    using System.Collections.Generic;

    public partial class Students
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Students()
        {
            this.Scenarios = new HashSet<Scenario>();
        }

        public int StudentsID { get; set; }
        public System.DateTime CreatedOn { get; set; }
        public string CreatedBy { get; set; }
        public System.DateTime UpdatedOn { get; set; }
        public string UpdatedBy { get; set; }
        public string StudentsCode { get; set; }
        public string StudentsName { get; set; }
        public bool isActive { get; set; }
        public string Comments { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Scenario> Scenarios { get; set; }
    }
}

我将数据加载为DataGridView中的列表。这很好用:

public partial class myCLASS : Form
{
    ...

    private void myCLASS_Load(object sender, EventArgs e)
    {

        myDataGridView.DataSource = (from x in _context.Students
        orderby x.MFlowOrdering
        select new { x.StudentsID,x.StudentsCode,x.StudentsName}).ToList();                                      
    }


    private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
      Students SelectedRow =(Students)(myDataGridView.CurrentRow.DataBoundItem);
      MessageBox.Show(SelectedRow.StudentsID);
    }
    ...
}

现在,我希望每次单击DataGridView中的一行(仅启用完整行选择)时,我想检索所选行后面的对象(Selected Class Students):

private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
  Students SelectedRow =(Students)(myDataGridView.CurrentRow.DataBoundItem);
  MessageBox.Show(SelectedRow.StudentsID);
}

我收到以下错误:

  

其他信息:无法将类型为'&lt;&gt; f__AnonymousType0`5 [System.Int32,System.Int32,System.String,System.String,System.Boolean]'的对象强制转换为类型'ClassLibrary.Students'。

我谷歌它似乎我使用正确的语法。

你可以请一下建议吗?

2 个答案:

答案 0 :(得分:3)

绑定网格时使用的是匿名类型。尝试在查询中选择学生,如下所示:

private void myCLASS_Load(object sender, EventArgs e)
{
    myDataGridView.DataSource = (from x in _context.Students
    orderby x.MFlowOrdering
    select new Students { 
                StudentsId = x.StudentsID, 
                StudentsCode = x.StudentsCode, 
                StudentsName = x.StudentsName
    }).ToList();                                      
}

<强>更新

如果首选匿名类型进行绑定,您可以使用动态关键字,而不是将数据绑定项投射到学生实体,如下所示:

private void myCLASS_Load(object sender, EventArgs e)
{
    myDataGridView.DataSource = (from x in _context.Students
    orderby x.MFlowOrdering
    select new { 
                StudentsId = x.StudentsID, 
                StudentsCode = x.StudentsCode, 
                StudentsName = x.StudentsName
    }).ToList();                                      
}

private void myDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
  dynamic SelectedRow = myDataGridView.CurrentRow.DataBoundItem;
  MessageBox.Show(SelectedRow.StudentsID);
}

答案 1 :(得分:1)

您需要为DGV配置DataBinding。请参阅此处的示例:DataGridView databinding