[英]Add column to Entity framework databound datagridview
在我的軟件中,我做了一些實體:
public abstract class Product
{
public int ProductId { get; set; }
public string name{ get; set; }
}
public class type1 :Product
{
public int number{ get; set; }
public string extradata{ get; set; }
public bool uitgeleend { get; set; }
}
public class type2 : Product
{
[Display(Name = "Merk en type")]
public string type { get; set; }
public string extradata{ get; set; }
public bool available{ get; set; }
}
為了顯示這是一個datagridview,我有這個表達式:
var results =db.producten.Where(c => c is type1|| c is type2).ToList();
dataGridView1.DataSource = results;
問題是,EF將額外數據作為類型1的額外數據放入表中,對於類型2的額外數據將其放在表格中。 當我想通過以下方式將我的額外數據添加到我的gridview中時
this.dataGridView1.Columns["extradata"].Visible = true;
我得到一個nullreferenceException,因為type2不包含這樣的列。 如何在我的datagridview中顯示該列,而又無需過多更改實體?
您可以為此目的使用DataTable
從列表中創建一個DataTable
,根據需要添加更多列,將DataTable
綁定到DataGridView
你完成了。
您可以嘗試一下,我認為:
不知道只是嘗試一下:
var results =db.producten.Where(c => c is type1|| c is type2).ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.