I have Gridview in ASP.net that displays data. Depending on the data it changes color and text depending on the value of the cell. This works fine when a column is NOT a template field.
//WORKS WHEN IS NOT A TEMPLATE FIELD
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[2].Text == "1")
{
e.Row.Cells[2].Text = "IN";
e.Row.Cells[2].BackColor = Color.Blue;
e.Row.Cells[2].ForeColor = Color.White;
}
}
Now I converted the Column in to a Template field and nothing works.
//DOEST NOT WORK WHEN IS a TEMPLATE FIELD
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[2].Text == "1")
{
e.Row.Cells[2].Text = "IN";
e.Row.Cells[2].BackColor = Color.Blue;
e.Row.Cells[2].ForeColor = Color.White;
}
}
I GOT THE COLOR WORKING, but now I need to change the Text to the following. IF statusID == 1 then display IN, else if statusID == 2 then display OUT
<asp:TemplateField HeaderText="StatusID" SortExpression="StatusID">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue = '<%# Bind("StatusID") %>'>
<asp:ListItem Value="1">IN</asp:ListItem>
<asp:ListItem Value="2">OUT</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Bind("StatusID") %>' ForeColor='<%# Convert.ToString(Eval("StatusID")) == "1" ? System.Drawing.Color.Green: Convert.ToString(Eval("StatusID")) == "2" ? System.Drawing.Color.Red: System.Drawing.Color.Purple%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Any of you know how to solve this issue. Thanks in advance.
The reason doesn't work in template column is status value is null. Try the following.
// In template column,
if (e.Row.RowType == DataControlRowType.DataRow)
{
var status = (Label)e.Row.FindControl("lblStatus");
if (status.Text == "1")
{
e.Row.Cells[2].Text = "IN";
e.Row.Cells[2].BackColor = Color.Blue;
e.Row.Cells[2].ForeColor = Color.White;
}
}
Or cast DataItem to appropiate object and get the status value.
// In template column,
if (e.Row.RowType == DataControlRowType.DataRow)
{
var obj = (MyObject)e.Row.DataItem;
if (obj.Status == 1)
{
e.Row.Cells[2].Text = "IN";
e.Row.Cells[2].BackColor = Color.Blue;
e.Row.Cells[2].ForeColor = Color.White;
}
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl=(Label)e.Row.FindControl("lblStatus");
if (lbl.Text == "1")
{
lbl.Text = "IN";
e.Row.Cells[2].BackColor = Color.Blue;
e.Row.Cells[2].ForeColor = Color.White;
}
}
I am a vb programmar here is a sample code.
If e.Row.RowType = DataControlRowType.DataRow Then
Dim abc As Label = TryCast(e.Row.FindControl("label1"), Label)
If abc.Text = "ADMIN" Then
e.Row.Cells(7).ForeColor = Drawing.Color.Blue
End If
End If
i really hope it works.
Use ItemTemplate
and surround Eval
with any HTML you need, for example, to change your color.
Text='<%# "Your HTML(use FONT COLOR=BLUE)" + Eval("HealthUnit") + "Close your HTML here" %>
as, here:
<asp:TemplateField HeaderText="Health Unit Website">
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl='<%# Eval("Website") %>'
tabindex="-1"
Target="_blank"
Text='<%# "<font color=blue><b>" +
Eval("HealthUnit") +
"</b></font>" %>'>
</asp:HyperLink>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" width="14%" />
</asp:TemplateField>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.