我试图在rowdatabound中的每个单元格上检查获取文本,这样有点对我不起作用,因为它给出了空白记录。下面是我的设计。在Rowdatabound上获取每个单元格的标签并检查值并相应地更改背景颜色。我需要找到行单元格。
<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>
答案 0 :(得分:0)
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;
}
}
}
当我这样做时,它改变整个行颜色,就像我尝试使用forecolor一样,每个单元格应根据状态具有不同的颜色
答案 1 :(得分:0)
如果你想要行单元格,试试这个
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";
}
}
}
编辑1: 你可能会认为这是一个hacky解决方案。以下代码将假定列数是固定的,它将使用数据表作为数据源,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
}
}