DataGridViewColumn.DataPropertyName属性 [英] DataGridViewColumn.DataPropertyName Property
问题描述
我使用DataSource属性
// dgvDealAsset是DataGridView
private void DealAssetListControl_Load(object sender,EventArgs e)
{
dgvDealAssets.AutoGenerateColumns = false;
dgvDealAssets.DataSource = DealAssetList.Instance.Values.ToList();
}
现在问题第一。我的集合的类不包含只能使用DataPropertyName映射到列的简单类型。这是集合中包含的类。
class MyClass
{
public String Name;
MyOtherClass otherclass;
}
class MyOtherClass
{
public String Name;
}
现在我将MyClass的属性绑定到列
col1.DataPropertyName =Name// Ok
col2.DataPropertyName =otherclass//不行 - 我将有空单元格
问题是我要显示otherclass.Name字段。但是如果我尝试写
col2.DataPropertyName =otherclass.Name
我得到空单元格。
我尝试手动设置列
private void DealAssetListControl_Load(object sender,EventArgs e)
{
dgvDealAssets.AutoGenerateColumns = false;
dgvDealAssets.DataSource = DealAssetList.Instance.Values.ToList();
//通过行迭代并手动设置列
foreach(dgvDealAssets.Rows中的DataGridViewRow行)
{
row.Cells [Column2]。 =((DealAsset)row.DataBoundItem).otherclass.Name;
}
但是这个foreach周期需要几分钟才能完成(2k个元素)。如何解决这个问题?
DataGridView不支持数据绑定到子属性。有关更多信息,请查看这篇文章
我喜欢使用CellFormatting事件的解决方案。
I have a DataGridView control and I want to populate it with data.
I use DataSource property
// dgvDealAsset is DataGridView
private void DealAssetListControl_Load(object sender, EventArgs e)
{
dgvDealAssets.AutoGenerateColumns = false;
dgvDealAssets.DataSource = DealAssetList.Instance.Values.ToList();
}
Now problem number one. The class of my collection does not contain only simple types that I can map to columns using DataPropertyName. This is the class that is contained in collection.
class MyClass
{
public String Name;
MyOtherClass otherclass;
}
class MyOtherClass
{
public String Name;
}
Now I am binding properties of MyClass to columns
col1.DataPropertyName = "Name" // Ok
col2.DataPropertyName = "otherclass" // Not OK - I will have empty cell
The problem is that I want to display otherclass.Name field. But if I try to write
col2.DataPropertyName = "otherclass.Name"
I get empty cell.
I tried to manually set the column
private void DealAssetListControl_Load(object sender, EventArgs e)
{
dgvDealAssets.AutoGenerateColumns = false;
dgvDealAssets.DataSource = DealAssetList.Instance.Values.ToList();
// iterate through rows and set the column manually
foreach (DataGridViewRow row in dgvDealAssets.Rows)
{
row.Cells["Column2"].Value = ((DealAsset)row.DataBoundItem).otherclass.Name;
}
But this foreach cycle takes about minute to complete (2k elements). How to solve this problem?
DataGridView doesn't support databinding to child properties. For more info, check this post
I like the solution that uses the CellFormatting event.
这篇关于DataGridViewColumn.DataPropertyName属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!