数据绑定组合框列每行一个DataGridView(不整列) [英] Databinding a combobox column to a datagridview per row (not the entire column)

查看:118
本文介绍了数据绑定组合框列每行一个DataGridView(不整列)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关于这几个职位,但搜索我几个小时后仍无法找到我所需要的。

There are a few posts about this, but after hours of searching I still can't find what I need.

在以下职位的答案几乎得到我想要的: 组合框在DataGridView的外键

The answer in the following post almost gets me what I want: Combobox for Foreign Key in DataGridView

问题1:

去关闭该例子,其中一个产品有很多许可证,我的数据库映射都多到一的关系,这意味着我的许可类包含引用到产品类。许可类没有一个属性为的ProductId由于可通过产品参考检索。我不想弄脏了许可类既引用到产品和财产的ProductId只是为了在用户界面更容易结合。

Going off that example where a Product has many Licenses, my database mappings are all many-to-one relationships which means my License class holds a reference to the Product class. The License class does not have a property for the ProductId since that can be retrieved via the Product reference. I don't want to muck up the License class with both a reference to Product and a ProductId property just to make binding in the UI easier.

我不能设置 DataPropertyName 来一个ID字段。它需要的类引用的名字,像这样:

Because of this I can't set the DataPropertyName to an Id field. It needs to be the class reference name like so:

DataGridViewComboBoxColumn dataGridViewComboBoxColumn = 
(DataGridViewComboBoxColumn)myDataGridView.Columns("LicenseComboBoxColumn");

dataGridViewComboBoxColumn.DataPropertyName = "License"; // not LicenseID

*的更新的* 我能得到这个没有通过指定Product.Id像这样DataPropertyName创建产品ID属性部分工作:

*Update* I was able to get this to partially work without creating the ProductId property by specifying the Product.Id as the DataPropertyName like so:

dataGridViewComboBoxColumn.DataPropertyName = "License.Id";

不过,这样做的时候,它打破了这引起了我的手动获取和设置单元格值的数据绑定。

However, when doing so, it broke databinding which caused me to manually get and set the cell value.

我也看到了张贴关于绑定到DataGridView单元格,但数据绑定符,当我做到这一点,数据源本身永远不会更新:

I've also seen posts about binding to the DataGridView cell, but databinding breaks when I do that and the datasource itself is never updated:

// populate the combo box with Products for each License

foreach (DataGridViewRow row in myDataGridViewProducts.Rows) 
{
    IProduct myProduct = row.DataBoundItem as IProduct;
    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells("myProductCol");
    cell.DataSource = getListOfILicenseObjectsFromDao(myProduct.Id);
    cell.Value = myProduct.License.Id;
}

也许我做错了什么,或者有不同的方式。任何人都可以在这里帮助?

Maybe I'm doing something wrong, or maybe there's a different way. Can anyone help here?

问2:

如何显示许可证为每个产品不同的名单? 换言之,许可证的组合框列表将成为网格中的每个产品的不同。我想做到这一点使用数据绑定,所以我没有获得和设置值自己。

How do I display a different list of Licenses for each Product? In other words, the combobox list of Licenses will be different for each Product in the grid. I'd like to do this using databinding so I don't have to get and set the values myself.

推荐答案

我发现自己的答案。我有同样的问题,前一阵子,发现在一些老code我挖出了解决方案。解决的办法是增加一个自我属性,我想数据绑定到组合框(在上面的例子中,这将是许可类)对象,并使用该属性作为ValueMember像这样:

I found the answer myself. I had this same issue a while ago and found the solution in some old code I dug up. The solution was to add a Self property to the object I wanted to databind to in the combobox (in the example above it would be the License class) and use that property as the ValueMember like so:

foreach (DataGridViewRow row in myDataGridViewProducts.Rows) 
{
    IProduct myProduct = row.DataBoundItem as IProduct;
    DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells("myProductCol");
    cell.DataSource = getListOfILicenseObjectsFromDao(myProduct.Id);
    cell.DataPropertyName = "License";        
    cell.DisplayMember = "Name";
    cell.ValueMember = "Self"; // key to getting the databinding to work
    // no need to set cell.Value anymore!
}

许可类现在看起来是这样的:

The License class now looks like this:

Public class License
{
    public string Name
    {
        get; set;
    }

    public ILicense Self
    {
        get { return this; }
    }

    // ... more properties
}

当然,我不得不弄脏了商务类使用指定属性自我,但这是更好的(少混乱程序员),比同时具有参考许可证,并在产品类中的LicenseId性海事组织。再加上它保持UI code多的要简单得多,因为没有必要手动获取和设置的值 - 只是数据绑定,做

Granted I had to "muck" up the Business classes with a property named Self, but that's much better (less confusing to the programmer) than having both a reference to License and a LicenseId property in the Product class IMO. Plus it keeps the UI code much much simpler as there's no need to manually get and set the values - just databind and done.

这篇关于数据绑定组合框列每行一个DataGridView(不整列)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆