2011-01-27 62 views
2

我将用示例快速解释我在找什么。DataGridView C#

说我有一个Employee类

public class Employee 
{ 
    int empId; 
    string empName; 
    Currency salaryCurrency; 
    string address; 
    decimal salaryAmnt; 
} 
public class Currency 
{ 
    string currencyCode; 
    string currencyName; 
    int numberOfDecimals; 
    .... 
    .... 
} 
public class Employees : Collection<Employee> 
{ 
    public Employees GetEmployees(); 
} 

在我的表单中我有一个DataGridView dgResult应显示employeeName,salaryAmount和currencyName。 (在货币类别内)。

我有两个问题:

1)我面临的挑战是,在如何引用属性的货币类中。因为我想要currencyName。

2)如何使我的DataGridView只显示这3列。我所做的是将Data Grid View的数据源分配给从GetEmployees函数返回的Employees集合。

dgResult.DatSource = new Employees().GetEmployees(); 

我正在使用Windows应用程序。

回答

0

我能想到的最简单的解决方案是,让您的Presenter提供EmployeeData集合,其中包含您所需的信息。

1

我一直使用这个解决方案,但我想知道是否有一种替代方案:

class Employee 
{ 

    public Currency Currency 
    { 
    get 
    { 
     return m_Currency; 
     } 
    } 

    public string CurrencyName 
    { 
    get 
     { 
     return this.Currency.Name; 
     } 
    } 
} 

你会然后绑定格列CurrencyName

我已经使用网格在过去,可以以“Currency.Name”作为显示成员。我从来没有得到这个在DataGridView上工作,但如果任何人都可以说如何做,那么请让我知道!

要只显示三列,你需要设置GridView控件不能自动生成列,然后指定每一列

+0

要隐藏某一列,您还可以在想要隐藏其列的属性前放置[[Browsable(false)]]。 – Blorgbeard 2011-01-27 16:11:17

1

我倾向于实现一个类,如:

public class EmployeeView { 
    private readonly Employee e; 
    public EmployeeView(Employee e) { this.e = e; } 
    public int Id {get {return e.empId; }} 
    public string Name {get {return e.empName; }} 
    public string Currency {get {return e.salaryCurrency.currencyName; }} 
    // ... 
} 

,并建立这些集合在DGV中使用。如果您希望DGV可编辑,您也可以分配设置器。