asp.net gridview绑定到特定类型的更深层属性

时间:2011-10-14 09:05:42

标签: asp.net gridview databound lync boundfield

我的网站有一个gridview 我将它绑定到从powershell

收回的数据集

在这个数据集中有很多不同的数据类型。

一切正常,但对于一个领域,我想将更深层的属性绑定到boundfield!

我这样绑定它:

C#

GridViewAgentGroups.DataSource = dt;
GridViewAgentGroups.DataBind();

标记

        <asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" />
        <asp:BoundField HeaderText="Service" ReadOnly="True"
                        DataField="Identity" />
        <asp:BoundField DataField="Description" HeaderText="Description" 
                        ReadOnly="True" />

服务的boundfield绑定到类型为"Microsoft.Rtc.Rgs.Management.RgsIdentity"

的数据

它包含instanceIDserviceID属性,而serviceID包含属性fullName

当我像"DataField="Identity"一样直接绑定它时,它会显示一个包含fullName的非常长的字符串!

有没有办法只绑定fullName?像"DataField="Identity.ServiceID.FullName"?在xml? (这不起作用:-)

1 个答案:

答案 0 :(得分:3)

是的TemplateFields是可行的,但它也取决于dataSource设计。看看这个样本:

标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <%#Eval("Name") %>
                <%#Eval("GroupName.Name") %>
                <%#Eval("GroupName.RegionName.Name") %>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码背后:

public class Region
{
    public string Name { get; set; }
}
public class Group
{
    public string Name { get; set; }
    private Region _region=new Region();
    public Region RegionName { get { return _region; } set { _region = value; } }
}
public class Product
{
    public string Name { get; set; }
    private Group _groupName = new Group();
    public Group  GroupName { get { return _groupName; } set { _groupName = value; } }
}
public class Products : List<Product>
{
}
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Region reg1 = new Region() { Name = "North" };
        Region reg2 = new Region() { Name = "East" };
        Group group1 = new Group() { Name="Group1", RegionName=reg1  };
        Group group2 = new Group() { Name = "Group2", RegionName=reg1 };
        Group group3 = new Group() { Name = "Group3", RegionName = reg2 };
        Products prod = new Products()
        {
                new Product(){ Name="Product1", GroupName=group1},
                new Product(){ Name="Product1", GroupName=group2},
                new Product(){ Name="Product2", GroupName=group3},
                new Product(){ Name="Product3", GroupName=group1},
                new Product(){ Name="Product2", GroupName=group2},
        };

        GridView1.DataSource = prod;
        GridView1.DataBind();
    }
}