开发者

DataGridView C#

开发者 https://www.devze.com 2023-02-06 21:32 出处:网络
I will quickly explain with an example to what I am looking for. Say I have a class Employee public class Employee

I will quickly explain with an example to what I am looking for.

Say I have a class Employee

public class Employee
{
     int empId;
     string empName;
     Currency salaryCurrency;
     string address;
     decimal salaryAmnt;
}
public class Currency
{
     string currencyCode;
     string currencyName;
     int numb开发者_开发百科erOfDecimals;
     ....
     ....
}
public class Employees : Collection<Employee>
{
   public Employees GetEmployees();
}

In my form I have a DataGridView dgResult which should display employeeName, salaryAmount and currencyName. (within the Currency class).

I have two question:

1)The challenge I am facing is, as in how to refer to the property within the Currency class. As I want the currencyName.

2) How to make my DataGridView to display only these 3 columns. What I did is assign the datasource of the Data Grid View to the collection of Employees returned from GetEmployees function.

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

I am working on a Windows application.


I always use this solution but I would like to know if there is an alternative:

class Employee
{

   public Currency Currency
   {
    get
     {
       return m_Currency;
      }
   }

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

You would then bind the grid column to CurrencyName

I have used grids in the past that can take a display member as "Currency.Name". I have never got this to work on a DataGridView but if anyone can say how to do it then please let me know!

To show only three columns you need to set the gridview to not automatically generate columns and then specify each column


I tend to implement a class like:

 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; }}
   // ...
 }

And build a collection of those to use in the DGV. You can assign setters too if you want the DGV to be editable.


The most simple solution I can think of is, let your Presenter offer a Collection of EmployeeData that contains exactly the information you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消