我的网站有一个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"
它包含instanceID
和serviceID
属性,而serviceID
包含属性fullName
!
当我像"DataField="Identity"
一样直接绑定它时,它会显示一个包含fullName的非常长的字符串!
有没有办法只绑定fullName?像"DataField="Identity.ServiceID.FullName"
?在xml? (这不起作用:-)
答案 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();
}
}