在DataGridView中隐藏类的某些属性 [英] Hide certain properties of a class in DataGridView

查看:147
本文介绍了在DataGridView中隐藏类的某些属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道类似于这个问题之前被问过,但我的是有点更加不同。我尝试了大多数先前建议的解决方案,但都没有工作。



所以问题是我有两个类如下所示:

  public class Dog 
{
public String Name {get;组; }
public int Age {get;组; }
}
public class Person
{
public String First_Name {get;组; }
public String Last_Name {get;组; }
public Dog dog {get; set;}
}

我有一个人列表< Person> ,我在datagridview中显示。
问题是当我显示它们时,我得到类似





但我需要的是:





我设法修复它的唯一方法是在To_String方法中只返回Dog名称,但它不适合我,因为我需要返回名称和狗年龄。



我希望你能理解我,并能帮助我。

解决方案

我对目前建议(甚至upvoted)的答案感到惊讶,建议创建另一个列表来更改显示表示的值。



你要求的是什么叫格式化,每个UI框架/组件都支持它。请参阅我对


I know that similar to this question was asked before, but mine is a little bit more different. I tried most of the previously suggested solutions, but none of them work.

So the problem is that I've got two classes as shown:

public class Dog
{
    public String Name { get; set; }
    public int Age{ get; set; }
}
public class Person
{
    public String First_Name { get; set; }
    public String Last_Name { get; set; }
    public Dog dog { get;set;}
}

Also, I have a list of People List < Person > that I am displaying in datagridview. The problem is when I display them I get something like

But what I need is:

The only way I managed to fix it is by returning only the Dog name in the To_String method, but it's not working for me because I need to return both of the name and the dog age.

I hope you understand me and can help me.

解决方案

I'm surprised by the currently suggested (and even upvoted) answer that suggests creating another list just to change the display representation of a value.

What are you asking is called formatting and is supported by every UI framework/component. See my answer to changing the value of a cell in gridview at runtime for more details.

All you need is to handle CellFormatting event and put inside the handler something like this:

var dog = e.Value as Dog;
if (dog != null)
{
    // Display whatever you like
    e.Value = dog.Name;
    e.FormattingApplied = true;
}

Full sample:

using System;
using System.Windows.Forms;

namespace Samples
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form();
            var dg = new DataGridView { Dock = DockStyle.Fill, Parent = form };
            dg.CellFormatting += (sender, e) =>
            {
                var dog = e.Value as Dog;
                if (dog != null) { e.Value = dog.Name; e.FormattingApplied = true; }
            };
            dg.DataSource = new[]
            {
                new Person { First_Name = "Ben", Last_Name = "Harison", dog = new Dog { Name = "Billy", Age = 50} },
                new Person { First_Name = "Rob", Last_Name = "Jonson", dog = new Dog { Name = "Puppy", Age = 25} },
            };
            Application.Run(form);
        }
    }

    public class Dog
    {
        public String Name { get; set; }
        public int Age { get; set; }
    }
    public class Person
    {
        public String First_Name { get; set; }
        public String Last_Name { get; set; }
        public Dog dog { get; set; }
    }
}

Result:

这篇关于在DataGridView中隐藏类的某些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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