如何DataGridView中的多个列绑定到一个自定义类的领域? [英] How to bind DataGridView's multiple columns to a custom class's fields?

查看:174
本文介绍了如何DataGridView中的多个列绑定到一个自定义类的领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个简单的类,它的实例清单:

Suppose I have a simple class, and list of its instances:

public class Class1
{
    public int a;
    public int b;
}
List<Class1> l = new List<Class1>();
l.AddRange(new[] { new Class1 { a = 1, b = 2 }, new Class1 { a = 3, b = 4 } });

我如何绑定列一个DataGridView的 A B 来呢?

我已经找到答案,但没有任何工作。这应该是简单的,但我不能弄明白。

I've found answers, but not any that work. This should be simple, but I can't figure it out.

推荐答案

假设你有列可乐 COLB ,设置其 DataPropertyName 属性,设置网格数据源

assuming that you have columns colA and colB, set their DataPropertyName property and set grid DataSource

colA.DataPropertyName = "a";
colB.DataPropertyName = "b";
grid.DataSource = l;

这篇关于如何DataGridView中的多个列绑定到一个自定义类的领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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