Inserting Columns to GridView dynamically
I have a gridview the page.. I added two columns!.. I didnt want to use the wizards to put data inside so I looked for some code over the internet that talks about feeding data dynamically to a gridview column Items..
I added some of the code if found and modified it to produce this:
<Columns>
<asp:TemplateField HeaderText="תגובות">
<ItemTemplate>
<asp:Label ID="Comments" runat="server" Text='<%# GetImage(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TicketReserved"))) %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Right" />
</asp:TemplateField>
<asp:TemplateField HeaderText="שם משתמש"><ItemTemplate>
<asp:Image ID="imgButton" runat="server"
ImageUrl='<%# GetImage(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TicketReserved"))) %>' ></asp:Image>
<br />
<asp:Label ID="Label1" runat="server" Text='<%# GetImage(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TicketReserved"))) %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
</asp:TemplateField>
</Columns>
The function:
GetImage(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TicketReserved"))) %>
Is a dummy function that I want to s开发者_JAVA技巧wap with properties from my code.
Here the code behind and what it should do.
List<ControlPanelMessages> allComments;
protected void Page_Load(object sender, EventArgs e)
{
allComments = ControlPanelMessages.GetAllControlPanelPosts();
foreach (var item in allComments)
{
Name=item.Name;
Comment=item.Comment;
Image =ResolveUrl(item.Img);
}
}
public string Name { get; set; }
public string Comment { get; set; }
public string Image { get; set; }
This method:
ControlPanelMessages.GetAllControlPanelPosts()
returns a list with after all the sql statements to get text for the users name, users avatar and users comment were applied.
My question is how can I insert those 3 properties safely instead of
GetImage(Convert.ToBoolean(DataBinder.Eval(Container.DataItem, "TicketReserved"))) %>
I want the Gridview add items to columns till the foreach statement ends!!!!
you can put the values in a datatable and bind the datatable to the gridview.
精彩评论