ASP.NET Change text and color in a gridview cell in a template field

I have a 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 the 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 have converted the column to the 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 have color work, but now I need to change the text to the next. IF statusID == 1, then display IN, otherwise, 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 problem. Thanks in advance.

+4
source share
4 answers

The reason does not work in the template column, the 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 run the DataItem to assign an object and get a status value.

GridViewRow.DataItem Property

 // 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; } } 
+7
source
 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; } } 
+2
source

I am a vb programmer here - 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 this works.

0
source

Use ItemTemplate and surround Eval whatever HTML you need, for example, to change 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> 
0
source

All Articles