Reputation: 2249
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.
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);
}
}
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.
How is it possible?
Upvotes: 0
Views: 116
Reputation: 3621
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:
Manually binding data to Gridview
Obviously you should and can bind your own collections to the DataSource
property.
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>
Upvotes: 2