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

发布于 2024-12-09 23:42:40 字数 981 浏览 1 评论 0原文

我的网站有一个 gridview 我将它绑定到从 powershell 接收的数据集,

该数据集中有很多不同的数据类型。

一切工作正常,但对于一个字段,我想将更深的属性绑定到边界字段!

我这样绑定它:

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" />

服务的绑定字段绑定到类型的数据:“Microsoft.Rtc.Rgs.Management.RgsIdentity”

它包含一个instanceIDserviceID 属性,并且 serviceID 包含属性 fullName

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

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

my webside has a gridview
i bind it to a dataset recived from powershell

in this dataset are a lot of different data types.

everything is working fine but for one field i would like to bind a deeper property to the boundfield!

i bind it like this:

c#

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

Markup

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

the boundfield of service binds to data of type: "Microsoft.Rtc.Rgs.Management.RgsIdentity"

it contains an instanceID and serviceID propertyand the serviceID contains a property fullName!

when i bind it directly like "DataField="Identity" it shows a very long string with the fullName included!

is there a way to only bind the fullName? like "DataField="Identity.ServiceID.FullName"? in xml? (this does not work :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云柯 2024-12-16 23:42:40

是的,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();
    }
}

Yes it is possible with TemplateFields but it depends upon the dataSource design too. Have a look at this sample:

Markup:

<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>

Code behind:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文