本文介绍了根据文本值自定义Gridview Label forecolor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用tu绑定gridview标签和从数据库中检索的数据,需要根据其绑定值自定义GridView下的Label颜色。
帮我...
以下是代码...
i use tu bind gridview labels with data retrieved from database and need to customise tho fore color of Label Under GridView depending on its binded value.
help me...
Below is the code ...
string strQry = "Select complain_date,complain_no,pno, problem, pcno, status from call_log where status NOT LIKE('solved') order by complain_date";
GlobalDs = GlobalConectionClass.ExecuteDataSet(strQry);
if (GlobalDs.Tables[0].Rows.Count > 0)
{
GridCall.DataSource = GlobalDs.Tables[0].DefaultView;
GridCall.DataBind();
}
GlobalDs.Dispose();
for (int i = 0; i < GridCall.Rows.Count - 1; i++ )
{
if (GridCall.Rows[i].Cells[4].Text == "hold")
{
GridCall.Rows[i].FindControl("lblStatus") //its color should be yellow
}else
if (GridCall.Rows[i].Cells[4].Text == "unsolved")
{
GridCall.Rows[i].FindControl("lblStatus") //its color should be RED
}
}
设计部分在这里...
design part is here...
<asp:GridView ID="GridCall" runat="server" CssClass="Grid"
AutoGenerateColumns="false" RowStyle-HorizontalAlign="Justify"
ShowHeader="true" ShowFooter="false" AllowPaging="false"
GridLines="Horizontal"
onselectedindexchanged="GridCall_SelectedIndexChanged" AutoGenerateSelectButton="true">
<HeaderStyle BackColor="AliceBlue" BorderStyle="Double" Height="25px" HorizontalAlign="Center" VerticalAlign="Middle" />
<RowStyle BackColor="Wheat" BorderStyle="Solid" Height="20px" VerticalAlign="Middle" HorizontalAlign="Center"/>
<Columns>
<asp:TemplateField ShowHeader="true" HeaderText="Status" >
<ItemTemplate> <asp:Label ID="lblStatus" runat="server" CausesValidation="false" CssClass="txt" Text='<%# Bind("status") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
推荐答案
// add RowDataBound event to your gridview
<asp:gridview id="GridCall" onrowdatabound="GridCall_RowDataBound" ...>
...
</asp:gridview>
//server side
protected void GridCall_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblStatus= (Label)e.Row.FindControl("lblStatus");
if(lblStatus.Text == "hold")
{
lblStatus.ForeColor = System.Drawing.Color.Yellow;
}
else if(lblStatus.Text == "unsolved")
{
lblStatus.ForeColor = System.Drawing.Color.Red;
}
}
}
希望它有所帮助。
hope it helps.
protected void GridCall_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (((Label)e.Row.FindControl("lblStatus")).Text == "hold")
{
e.Row.Cells[1].ForeColor= System.Drawing.Color.Yellow;
}
else if (((Label)e.Row.FindControl("lblStatus")).Text == "unsolved")
{
e.Row.Cells[1].ForeColor= System.Drawing.Color.Red;
}
}
}
这篇关于根据文本值自定义Gridview Label forecolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!