在对象列表隐藏附加属性被绑定到一个gridview [英] Hiding Additional Attributes in a list of objects to be binded to a gridview
问题描述
我有型的用户现场负载有许多属性,我想隐藏一些属性,因为我不希望我的网格视图显示所有这些对象的列表。
I have a list of objects of type user site load that has many attributes , i want to hide some of these attributes because i do not want My grid view to show all of these.
我该怎么办呢?我使用 GridView.DataSource = MyListOfUserSiteLoadObjects;
How can i do it? I use GridView.DataSource=MyListOfUserSiteLoadObjects;
public partial class Create : System.Web.UI.Page
{
public List<Entity.UserSiteLoad> MyTempList = new List<Entity.UserSiteLoad>();
public Entity.UserSiteLoad usl = new Entity.UserSiteLoad();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["MyApplianceList"] = MyTempList;
}
}
protected void BtnAddNext_Click(object sender, EventArgs e)
{
List<Entity.UserSiteLoad> LstUsl = (List<Entity.UserSiteLoad>)Session["MyApplianceList"];
usl.Applianc = new Entity.Appliance();
usl.Applianc.Name = DDLAppName.Text;
usl.Quantity = Convert.ToInt32(QtyTB.Text);
usl.DayTime = Convert.ToInt32(DayTymTB.Text);
usl.BackUpTime = Convert.ToInt32(BackUpTymTB.Text);
if (LstUsl.Count != 0)
{
for (int rowIndex = 0; rowIndex < LstUsl.Count; rowIndex++)
{
string AppName = LstUsl[rowIndex].Applianc.Name;
if (AppName == DDLAppName.Text)
{
LstUsl.Remove(LstUsl[rowIndex]);
}
}
}
if (LstUsl.Count == 0 || LstUsl.Count > 0)
{
LstUsl.Add(usl);
}
AllItems.DataSource = LstUsl;
AllItems.DataBind();
AllItems.Visible = true;
Response.Write("It has: " + AllItems.Attributes.Count);
}
}
第二个问题:我要一列添加到我的 GridView控件名为设备名称并将其值设置为 USL .Applianc.Name Applianc 里面聚集对象的用户站点的负载对象
2nd problem: I want to add a column to my GridView named "Appliance name" and set its value to usl.Applianc.Name , Applianc is an aggregated object inside user site load object.
这怎么可能?
推荐答案
您应该配置的AutoGenerateColumns
的属性&LT; ASP:GridView的/方式&gt;
元素假
和配置/手动绑定列
You should configure the AutoGenerateColumns
property of the <asp:GridView />
element to false
and configure/bind the columns manually.
下面的堆栈会给你如何做到这一点的想法:
The following stack will give you an idea of how to do that:
显然,你应该和可以绑定自己的集合到数据源
属性。
Obviously you should and can bind your own collections to the DataSource
property.
我觉得你的code,后面可以保持原样,你只需要改变一些标记:
I think your code-behind can stay as is and you only have to change some markup:
<asp:GridView ID="AllItems" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="PropA" HeaderText="Property A" />
<asp:BoundField DataField="PropB" HeaderText="Property B" />
<!-- ... -->
</Columns>
</asp:GridView>
这篇关于在对象列表隐藏附加属性被绑定到一个gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!