0

I have a DataGridView being populated using CoolStorage (ORM) CSList class as its DataSource. It is displaying Contacts and it's the ContactType column that instead of displaying the underlying ContactTypeName just displays "ContactType" for every record.

I cannot change the value of that cell by stepping through the rows as it's data bound, although when hovering over DataSource I can see that it has the ContactTypeName is available as a property of ContactType.

Is there a way I can change what value is displayed in the ContactType cells (I'm thinking something like an equivalent to ComboBox's DisplayMember) without having to manually constuct a DataTable from my result set?

Many thanks.

4

2 回答 2

3

在不手动设置列的情况下,我知道的唯一ToString()一种方法是覆盖类的功能ContactType

但是,如果要对其进行编辑,则需要手动设置列。

由于某种原因,几乎所有其他我处理过的网格 UI 都需要手动设置列。

另一种选择是绑定到将 ContactName 作为属性而不是相关对象公开的包装类(基本上是 ViewModel)。您甚至可以使用该模型进行双向绑定。

于 2012-12-18T22:12:52.527 回答
1

您是否考虑过使用CSListGeneric而不是CSList

然后,您可以使用System.Linq选择要在 中显示的内容DataGridView

class Program
{
    class ContactType
    {
        public string Name { get; set; }
    }
    class Contact
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ContactType ContactType { get; set; }
    }
    static void Main(string[] args)
    {
        CSList<Contact> contacts = new CSList<Contact>();
        //add contacts to the list
        var x = from c in contacts
                select new {
                    c.Id,
                    c.Name
                    ContactTypeName=c.ContactType.Name
                };
    }
}
于 2012-12-18T22:22:29.650 回答