填充用户控件的GridView使用对象的名单 [英] Populate a UserControl Gridview with a List of Objects

查看:81
本文介绍了填充用户控件的GridView使用对象的名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个属性code和功放称为理由对象的名单; 文本。我想用这个来填补一个GridView的用户控件。但是,我不知道如何在GridView链接到原因的列表和实际设置从对象使用的数据。

I have a List of an object called "Reasons" that contains two properties "Code" & "Text". I want to use this to fill a UserControl of a Gridview. However, I don't understand how to link the gridview to the List of Reasons and actually set which data to use from the object.

我将承担的办法是设置数据源的列表,但是,不工作,因为它似乎填充任何行GridView控件。是否有解决这个问题的更好方法?

I would assume the approach would be to set the datasource to the List, however, that is not working as it does seem populate the gridview with any rows. Is there a better approach to this problem?

推荐答案

我假设你在winform C#这样做。它应该是在C#codebehind为asp.net相当类似。
下面是一些示例code,你可以轻松地定制你的obj类型:

I am assuming you're doing this in winform C#. It should be fairly similar in C# codebehind for asp.net. Here's some sample code you can easily customize to your obj type:

/// <summary>
/// The test class for our example.
/// </summary>
class TestObject
{
    public string Code { get; set; }
    public string Text { get; set; }
}

void PopulateGrid()
{
    TestObject test1 = new TestObject()
    {
    Code = "code 1",
    Text = "text 1"
    };
    TestObject test2 = new TestObject()
    {
    Code = "code 2",
    Text = "text 2"
    };
    List<TestObject> list = new List<TestObject>();
    list.Add(test1);
    list.Add(test2);

    dataGridView1.DataSource = list;
}

这篇关于填充用户控件的GridView使用对象的名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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