【问题标题】:How to display custom field type on data grid view? [C# / WinForms]如何在数据网格视图上显示自定义字段类型? [C#/WinForms]
【发布时间】:2016-11-21 20:44:54
【问题描述】:

我有一个员工类,它的字段类型为 ISalary(接口)。在数据网格视图中,我想显示该 salary,但我得到的是空字段。 有没有办法在数据网格视图中显示“自定义类型字段”?调用 toString 方法会有所帮助,但我不明白该怎么做。 这是我绑定数据的方式:

 employeeBindingSource.DataSource = employeesList;

该列表中的所有字段,ofc 不为空。这是我的一些课程,我想显示哪些列表:

 public class Employee
 {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public int DepartmentId { get; set; }

        // how to display this?
        public ISalary Salary { get; set; }

        ...
 }

我做了一些研究,但找不到任何例子。或者可能是不够努力。:/

【问题讨论】:

  • 看看这个帖子:How to bind a column from second level list on bindsource in winforms datagridview 或这个帖子:Show Properties of a Navigation Property in DataGridView (Second Level Properties),在这种情况下,您使用的是ISalary 接口,最好的选择是使用CellFormatting 或使用ComboBox 专栏。
  • 如果你想遵循覆盖ToString,你应该知道ISalarySalary属性中的实现类型并覆盖该类型的ToString方法,但不是一个好主意,因为就像您使用该类型而不是接口一样,您的应用程序将失去使用ISalary 接口的目标。
  • @RezaAghaei 我只是想知道,为什么在不知道具体实现类型的情况下无法覆盖 ToString?我的意思是它应该可以工作.. 是的,如果 ToString 方法没有被覆盖,它不会给出正确的结果,但仍然..
  • 接口不能包含实现。实现属于类,如果您知道具体类型,则可以覆盖实现。您也可以使用抽象类型而不是接口,然后抽象类可以覆盖ToString 的实现。您还可以将只读属性添加到 Employee 类,该类返回一个已知的薪水字符串属性,如下所示:this.Salary.Something。我相信您会在链接的帖子中找到有用的选项:)
  • 我发布了一个答案作为结论:)

标签: c# .net winforms user-interface


【解决方案1】:

由于您使用接口作为属性类型,因此您不能在不知道具体类型的情况下为该ISalary 类型覆盖ToString 方法。因此,如果您知道一个 Salary 类实现了该接口并在运行时代替该接口使用,您可以覆盖该类型的 ToString

但是,一般来说,依赖具体类型的ToString 并不是一个好主意,这样您的程序将紧紧依赖具体类型,并且会失去使用接口的目标。

相反,您可以使用以下任一选项:

  • 使用CellFormatting事件提供显示值。
  • 使用包含List<Salary>DataGridViewComboBoxColumn,但将其显示样式属性设置为nothing 以不显示下拉按钮。
  • Employee 类中使用只读属性,该属性返回ISalary 的已知属性,例如return this.Salary.SomeProperty;

还有更多选择。要查看其他选项和示例,请查看此帖子:How to bind a column from second level list on bindsource in winforms datagridview 或此帖子:Show Properties of a Navigation Property in DataGridView (Second Level Properties)

【讨论】:

    【解决方案2】:

    这应该适合你:

    public ISalary Salary 
    { 
    get{ return this.Salary.ToString(); }; 
    set; 
    }
    

    要使用 ToString(),您只需将它附加到要作为字符串返回的对象的末尾。

    ToString() 函数见官方 MSDN:

    Object.ToString Method

    【讨论】:

    • 嗯,这可能不是那么容易。我试过了,得到“不能隐式地将字符串转换为 ISalary”,因为我猜返回类型应该匹配。 Resharper 只提供了一种解决方案:将属性类型更改为字符串..
    • 也许您也可以在实现 ISalary 的类中重写 ToString 以获取您的自定义视图
    【解决方案3】:
    interface ISalary
    {
        public decimal Amount { get; set; }
        public string GetSalaryString();
    }
    

    代码:

    List<Employee> lstEmployee = GetEmployeeList();
    dataGridView1.Rows.Clear();
    
    if (lstEmployee.Count == 0)
        return;
    
    foreach (var employee in lstEmployee)
    {
        DataGridViewRow dgvr = dataGridView1.Rows[dataGridView1.Rows.Add()];
        dgvr.Cells[colnId.Index].Value = employee.Id;
        dgvr.Cells[colnName.Index].Value = employee.Name;
        dgvr.Cells[colnGender.Index].Value = employee.Gender;
        dgvr.Cells[colnDepartmentId.Index].Value = employee.DepartmentId;
        dgvr.Cells[colnSalary.Index].Value = employee.Salary.GetSalaryString();
    }
    

    【讨论】:

    • 那么在这种情况下,您必须以编程方式添加所有内容,包括列,并且没有任何绑定源。但是,无论如何,感谢您提供替代解决方案。
    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多