如何设置网格视图单元格值颜色?

编程入门 行业动态 更新时间:2024-10-10 11:29:41
本文介绍了如何设置网格视图单元格值颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我有一个通过存储过程动态填充的网格视图 我想问一下如何在填充网格视图时设置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 &gt; 41 &amp;&amp; value &lt; 60 ) { e.Item.BackColor = System.Drawing.Color.Red; } } }

更多推荐

如何设置网格视图单元格值颜色?

本文发布于:2023-11-23 04:17:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620057.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   视图   单元格   如何设置   颜色

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!