DatagridView +自定义对象绑定

时间:2008-12-16 06:53:32

标签: c# bindinglist

我在.NET 3.5中有一个datagridview,需要显示以下列:

ID,Name,ID2,Name2

此datagridview绑定如下:

BindingList<Class1> source = new BindingList<Class1>();
source.Add(new Class1 { ID = "1", Name = "Test", Class2Ref = new Class2 { ID2 = "Class2 ID" , Name2 = "Class2 Name"} });
dataGridView1.DataSource = source;


public class Class1
{
    public string ID
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    public Class2 Class2Ref
    {
        get;
        set;
    }
}


public class Class2
{
    public string ID2
    {
        get;
        set;
    }

    public string Name2 
    { 
        get; 
        set; 
    }
}

问题是ID2和Name2在datagridview中没有显示任何值。我在设计时在Datagridview中创建了列,并且对应于ID2和Name2的列已经被赋值为:Class2Ref.ID2和Class2Ref.Name2

请注意,在UI端,用户无论ID2和Name2是Class1还是Class2的属性,但在Entity设计级别,它确实有所不同。请告诉我如何在Datagrid中显示2个属性,这些属性不是直接在类中提供,而是通过引用提供。

1 个答案:

答案 0 :(得分:1)

你可能会看到这对你有用(它对我有用):

<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    public class Class1
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public Class2 Class2Ref { get; set; }
    }

    public class Class2
    {
        public string ID2 { get; set; }
        public string Name2 { get; set; }
    }

    void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            myGrid.DataSource = new[] 
            { 
                new Class1() 
                {
                    ID = "1",
                    Name = "Name1",
                    Class2Ref = new Class2() 
                    {
                        ID2 = "IDRef1",
                        Name2 = "NameRef1"
                    }
                },
                new Class1() 
                {
                    ID = "2",
                    Name = "Name2",
                    Class2Ref = new Class2() 
                    {
                        ID2 = "IDRef2",
                        Name2 = "NameRef2"
                    }
                }
            };
            myGrid.DataBind();
        }
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DataGrid runat="server" ID="myGrid" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateColumn>
                    <HeaderTemplate>
                        ID
                    </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("ID") %>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn>
                    <HeaderTemplate>
                        Name
                    </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn>
                    <HeaderTemplate>
                        ID2
                    </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("Class2Ref.ID2") %>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn>
                    <HeaderTemplate>
                        Name2
                    </HeaderTemplate>
                    <ItemTemplate>
                        <%# Eval("Class2Ref.Name2") %>
                    </ItemTemplate>
                </asp:TemplateColumn>
            </Columns>
        </asp:DataGrid>
    </div>
    </form>
</body>
</html>