winForms + DataGridView绑定到List< T> [英] winForms + DataGridView binding to a List<T>

查看:129
本文介绍了winForms + DataGridView绑定到List< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个 List< T> 绑定到一个DataGridView控件,我没有任何运气创建自定义绑定。

I'm trying to bind a List<T> to a DataGridView control, and I'm not having any luck creating custom bindings.

我试过:

gvProgramCode.DataBindings.Add(new Binding("Opcode",code,"Opcode"));

它引发异常,表示该属性名称没有找到。

It throws an exception, saying that nothing was found by that property name.

有问题的列的名称是操作码。 列表< T> 中的属性名称是操作码。

The name of the column in question is "Opcode". The name of the property in the List<T> is Opcode.

ANSWER EDIT :问题是我没有我的类中的可绑定字段作为属性,只是公共字段...显然它不反映在字段,只是属性。

ANSWER EDIT: the problem was that I did not have the bindable fields in my class as properties, just public fields...Apparently it doesn't reflect on fields, just properties.

推荐答案

网格上的属性是否与Opcode绑定?如果要直接绑定到列表,您只需要DataSource = list。数据绑定允许自定义绑定。你试图做一些除数据源以外的事情吗?

Is the property on the grid you are binding to Opcode as well?.. if you want to bind directly to List you would just DataSource = list. The databindings allows custom binding. are you trying to do something other than the datasource?

你正在得到一堆空行?自动生成的列有名称吗?你验证的数据是否在对象中(不只是string.empty)?

You are getting a bunch of empty rows? do the auto generated columns have names? Have you verified data is in the object (not just string.empty) ?

    class MyObject
    {
        public string Something { get; set; }
        public string Text { get; set; }
        public string Other { get; set; }
    }

    public Form1()
    {
        InitializeComponent();

        List<MyObject> myList = new List<MyObject>();

        for (int i = 0; i < 200; i++)
        {
            string num = i.ToString();
            myList.Add(new MyObject { Something = "Something " + num , Text = "Some Row " + num , Other = "Other " + num  });
        }

        dataGridView1.DataSource = myList;
    }

这应该可以正常...

this should work fine...

这篇关于winForms + DataGridView绑定到List&lt; T&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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