[英]check the text of labels in gridview and change its background color on condition
I tried to check get text on each cell in rowdatabound so that kinda didn't work for me as it gave blank record. 我试图检查是否在rowdatabound中的每个单元格上获取文本,以便它对我来说不起作用,因为它给出了空白记录。 Below is my design.On Rowdatabound get the label of each cell and check the values and change the background colour accordingly.
下面是我的设计。在Rowdatabound上获取每个单元格的标签并检查值并相应地更改背景颜色。 I need to find row cell.
我需要找到行单元格。
<asp:GridView ID="grv_taskfilter" runat="server" AutoGenerateColumns="False"
style="margin-top: 0px" OnRowDataBound="gv_RowDataBound" BackColor="White"
BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="2"
CellSpacing="1" Font-Size="XX-Small" GridLines="None" >
<Columns>
<%--<asp:TemplateField ItemStyle-Width="30px" HeaderText="Project ID">
<ItemTemplate>
<asp:Label ID="lblProjectID" runat="server"
Text='<%# Eval("ID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbl_ProjectNamehead" Text='Project Name' runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<a href="Task_description.aspx?id=<%#Eval("ProjectName")%>&flag=0" > <%# Eval("ProjectName") %></a>
<%-- <asp:LinkButton ID="lbl_ProjectName" Text='<%# Eval("ProjectName") %>'
PostBackUrl='<%# "~/Task_description.aspx?RowIndex=" & Container.DataItemIndex %>'
runat="server"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbl_ProjectNoshead" Text='Project nos' runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblProjectNos" Text='<%# Eval("ProjectNos") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbl_QuestionScripting" Text='Questionnaire Submission for sripting' runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblQuestion_Scripting" Text='<%# Eval("Questionnaire_Submission_for_sripting") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lbl_Programing" Text='Programing of questionnaire and/or ConJoint Design/Email Send out' runat="server"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblPrograming_questionnaire" Text='<%# Eval("Programing_of_questionnaire_and_or_ConJoint_Design_Email_Send_out") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
Label lbl1 = (Label)e.Row.FindControl("lbl_Question_Scripting");//use e.rows.cell[]
if (lbl1.Text == "Not yet started")
{
e.Row.Cells[i].BackColor = Color.Gray;
}
if (lbl1.Text == "In progress")
{
e.Row.Cells[i].BackColor = Color.Orange;
}
if (lbl1.Text == "Alert")
{
e.Row.Cells[i].BackColor = Color.Yellow;
}
if (lbl1.Text == "Missed deadline")
{
e.Row.Cells[i].ForeColor = Color.Red;
// e.Row.Cells[i].BackColor = Color.Red;
}
if (lbl1.Text == "Not Applicable")
{
e.Row.Cells[i].BackColor = Color.BlueViolet;
}
}
}
when i do this it changes the entire row color like i tried with forecolor here every cell should have different colour depending on status 当我这样做时,它会更改整个行的颜色,就像我尝试使用前景色一样,这里每个单元格的颜色应取决于状态
If you want the row cell, try this 如果需要行单元格,请尝试此
protected void grv_taskfilter_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow grv = (GridViewRow)e.Row;
Label lbl1 = (Label)grv.FindControl("lblQuestion_Scripting");
if (lbl1.Text == "Condition_Text")
{
TableCell rowCell = (TableCell)lbl1.Parent;
rowCell.Style["background"] = "red";
}
}
}
EDIT 1: You may consider this a hacky solution. 编辑1:您可能会认为这是一个hacky解决方案。 The following code will assume that the number of columns is fixed, it will use a datatable as datasource, and css class for the cell background
以下代码假定列数是固定的,它将使用数据表作为数据源,并使用css类作为单元格背景
- -
protected void grv_taskfilter_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow grv = (GridViewRow)e.Row;
DataRowView drv = (DataRowView)e.Row.DataItem;//you'll need to cast to another object if you're not using datatable
//No need to check the value of the label. It should have been handled in step 2 above
grv.Cells[0].CssClass = drv["Status_CSS"].ToString();// Fixed colums: Assuming that cell 0 will always be for Status
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.