如何设置网格视图单元格值颜色? [英] How to I set a Grid View Cell Value colour?
本文介绍了如何设置网格视图单元格值颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我有一个通过存储过程动态填充的网格视图
我想问一下如何在填充网格视图时设置column17的值的颜色?
例如,对于介于1 - 20之间的值范围,我想将颜色设置为是绿色,21-40之间的值是橙色,41 - 60是红色吗?
我填写网格视图的代码:
Hi Members,
I have a grid view that is populated dynamically through a stored procedure
I would like to ask how can I set the colour of the value for column17 upon populating the gridview?
For example, for value range between 1 - 20 I would like to the colour to be green, value between 21-40 to be orange and 41 - 60 to be red?
My codes to populate the gridview:
using (SqlCommand cmd = new SqlCommand(spretrieve, conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@param1", SqlDbType.VarChar).Value = DATE;
cmd.Parameters.Add("@param2", SqlDbType.VarChar).Value = Key;
string query = cmd.CommandText;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Gridview1.DataSource = ds.Tables[0];
Gridview1.DataBind();
推荐答案
Hello Permalink,
使用Gridview控件的RowDatabound事件非常简单
选择网格视图控件=>属性=>事件=> rowDatabound双击并过去代码如下:
protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
if(e .Row.RowType == DataControlRowType.DataRow)
{
int iCellValue = Convert.ToInt32(e.Row.Cells [16] .Text);
if(iCellValue> = 1 && iCellValue< = 20)
{
e.Row.Cells [16] .BackColor = System.Drawing。 Color.Green;
}
else if(iCellValue> = 21 && iCellValue< = 40)
{
e.Row.Cells [16] .BackColor = System.Drawing.Color.Orange;
}
if(iCellValue> = 41 && iCellValue< = 60)
{
e.Row.Cells [16] .BackColor = System.Drawing.Color.Red;
}
}
}
Hello Permalink,
Its very easy with RowDatabound event of the Gridview Controll
Select The Grid View Control =>Properties=>events=>rowDatabound double click and past the code below :
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int iCellValue=Convert.ToInt32(e.Row.Cells[16].Text);
if (iCellValue >= 1 && iCellValue <= 20)
{
e.Row.Cells[16].BackColor = System.Drawing.Color.Green;
}
else if (iCellValue >= 21 && iCellValue <= 40)
{
e.Row.Cells[16].BackColor = System.Drawing.Color.Orange;
}
if (iCellValue >= 41 && iCellValue <= 60)
{
e.Row.Cells[16].BackColor = System.Drawing.Color.Red;
}
}
}
我在我的GridView中完成了这个工作
I had done this in my GridView
<asp:TemplateField HeaderText="Correct Filling No">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text='<%# Bind("CorrectFillingNo") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlcorrectFillingNo" runat="server" SelectedValue='<%# Eval("CorrectFillingNo").ToString()=="" ? "Yes" : Eval("CorrectFillingNo") %>'
CssClass="textareadropdown" BackColor='<%# Eval("CorrectFillingNo").ToString()=="No" ? System.Drawing.Color.Red : System.Drawing.ColorTranslator.FromHtml("#F3F3F3") %>'>
<asp:ListItem Selected="True">Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
<asp:ListItem>N/A</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
OnRowDataBound事件你可以成为合作伙伴对于使用条件的单元格,请参阅下面的示例。
Hi,
OnRowDataBound event you can make the color against cell using conditions, Refer below sample.
protected void gv_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.DataRow)
{
int value=Convert.ToInt32(((Label)e.Row.FindControl("lblValue")).Text); // If you are using a TemplateField
if(value > 1 && value < 20 )
{
e.Item.BackColor = System.Drawing.Color.Green;
}
else if(value > 21 && value < 40 )
{
e.Item.BackColor = System.Drawing.Color.Orange;
}
else if(value > 41 && value < 60 )
{
e.Item.BackColor = System.Drawing.Color.Red;
}
}
}
这篇关于如何设置网格视图单元格值颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文