Reputation: 649
I have an fieldname called has_Amenities. This fieldname can take one, two, three, four, or five values at same time.
The values are water, electricity, pets, Full hookup, All
If has_Amenities = "electricity", then show electricity icon but also show blank icons for pets, full hookup and water tap.
If has_Amenities = "pets", show pets icon as well as blank icons for electricity, full hookup and water
If has_Amenities = "water", show water icon and also show blank icons for pets, full hookup and electricity.
If has_Amenities = "full", show full hookup icon and also show blank icons for pets, water and electricity.
If has_Amenities = "All", then it means it has electricity, water, full hookup and pets. Show all four icons.
We are using gridview and gridview control id is gridview1.
The code below works by displaying just one value - Electricity.
<asp:TemplateField HeaderText="Facility Has">
<ItemTemplate>
<asp:Image ID="ImageDetailItem" width="22" height="22" ImageUrl='<%# IIF(CONVERT.ToString(Eval("has_Amenities")) = "Electricity", "~/images/icon_amps_50.gif", Eval("location","~/images/icon_waterfront_no.gif")) %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
This is not the correct solution.
How do I use all the IF conditions mentioned above and how do I show all four icons in one cell?
Below is an image of how they are laid out.
Upvotes: 0
Views: 1217
Reputation: 1
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// set formatting for the category cell
TableCell cell = e.Row.Cells[2];
// set formatting for value cells
for (int i = 2; i < e.Row.Cells.Count; i++)
{
cell = e.Row.Cells[i];
int c_val = int.Parse(e.Row.Cells[i].Text);
if (c_val == 0)
{
//cell.BackColor = System.Drawing.Color.Red;text-align: center;
cell.ForeColor = System.Drawing.Color.Red;
cell.Attributes.Add("style", "text-align: center;");
cell.Text = "<i class='fa fa-remove'></i>";
//cell.Attributes.Add("Class", "fa fa-remove");
}
else
{
cell.ForeColor = System.Drawing.Color.Green;
cell.Attributes.Add("style", "text-align: center;");
cell.Text = "<i class='fa fa-check'></i>";
//cell.Attributes.Add("Class", "fa fa-check");
}
}
}
else if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 2; i < e.Row.Cells.Count; i++)
{
string Header_NewText = e.Row.Cells[i].Text;
e.Row.Cells[i].Text = Header_NewText.Replace(",", "<br/>");
}
}
}
Upvotes: 0
Reputation: 556
I did similar things in asp.net webforms using c# like this (i hope VB is not much different)
<asp:TemplateField>
<ItemTemplate>
<asp:Image runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Electricity")? "~/electricity.gif":"~/blank.gif" %>"/>
<asp:Image ID="Image1" runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Pets")? "~/pets.gif":"~/blank.gif" %>"/>
<asp:Image ID="Image2" runat="server" ImageUrl="<%# ProtectedIsVisible(Item.HasAmenties,"Water")? "~/water.gif":"~/blank.gif" %>"/>
</ItemTemplate>
</asp:TemplateField>
The page class had protected method,containing all logic
protected bool ProtectedIsVisible(string[] values,string tag)
And I used ItemType property of GridView, set to strongly typed model class,something like
class Model{ public string[] HasAmenties {get;set; }
<asp:GridView ItemType="MyApplication.Model" runat="server" ID="MyGridView">
Upvotes: 0
Reputation: 3269
You can put this logic in RowDataBound
event. The RowDataBound
event is raised when a data row (represented by a GridViewRow
object) is bound to data in the GridView
control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.
Here is example code that you can use for your problem.
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim drv As Common.DbDataRecord = CType(e.Row.DataItem, Common.DbDataRecord)
e.Row.Cells(1).Text = "<i>" & e.Row.Cells(1).Text & "</i>"
Image img = e.row.Cells(7).FindControl("image1")
if drv("has_Amenities") = "Electricity" Then
img.ImageUrl = "~/images/icon_amps_50.gif"
elseif
' ////////////////////////////////
' You can place other logic here...
End If
End Sub
Upvotes: 1