Gridview变色问题,貌似有点难度?

空白画映 2010-02-04 09:49:06
Gridvie:

公司名称 联系人
A A

B B

C C

D D

SQL数据库:

Name Contact Type 三个字段

Tpye值 是A类或者B类

Gridvie 绑定了 Name和Contact 现在想通过Type来判断如果是A类的客户 Gridvie上对应的整行
都变成红色 而不是某个单元格变成红色

也看过别人提过类似的问题,大部分的判断条件是绑定到Gridvie上的某个字段来做判断。如果我的条件是没有绑定到Gridvie上譬如用Type来判断 应该怎么写呢?
...全文
234 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhoulihong_1986 2010-02-05
  • 打赏
  • 举报
回复
protected void gvw_SoShop_RowDataBound(object sender, GridViewRowEventArgs e)
{
///添加鼠标移上变色
if (e.Row.RowType == DataControlRowType.DataRow)
{
///鼠标经过时,行背景色变
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#F5EAE9'");
///鼠标移出时,行背景色变
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
heyalin 2010-02-05
  • 打赏
  • 举报
回复
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells["Type"].Text=="A")
{
e.Row.BackColor=Color.Red;
}
}
}
aellonxie 2010-02-05
  • 打赏
  • 举报
回复
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lbtn = (LinkButton)e.Row.Cells[13].Controls[0];
lbtn.Attributes.Add("onclick", "return confirm('确定删除 " + (e.Row.Cells[1].Text) + " 项吗?');");

}
if (Convert.ToString(e.Row.Cells[8].Text) == "取消")
{
e.Row.Style.Value = "color:LightBlue";
e.Row.Cells[10].ForeColor = System.Drawing.Color.LightBlue;
e.Row.Cells[11].ForeColor = System.Drawing.Color.LightBlue;
e.Row.Cells[12].ForeColor = System.Drawing.Color.LightBlue;
e.Row.Cells[13].ForeColor = System.Drawing.Color.LightBlue;
e.Row.Cells[14].ForeColor = System.Drawing.Color.LightBlue;
e.Row.Cells[15].ForeColor = System.Drawing.Color.LightBlue;
LinkButton lbtn = (LinkButton)e.Row.Cells[13].Controls[0];
lbtn.Text = "已删除";
lbtn.Attributes.Add("onclick", "return false;");

}


}


这个功能我一直在用,其实很简单,没有添加LinkButton 这些控件的行数e.Row.Style.Value = "color:LightBlue";
就能搞定,但是有LinkButton这些控件的,那么就要一列列的改了 e.Row.Cells[13].ForeColor = System.Drawing.Color.LightBlue;
,去感觉一下吧,很简单的
rpoplar 2010-02-05
  • 打赏
  • 举报
回复
这个就可以嘛![Quote=引用 3 楼 criedshy 的回复:]
C# codeprotectedvoid GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{if (e.Row.RowType== DataControlRowType.DataRow)
{if(e.Row.Cells["Type"].Text=="A")
?-
[/Quote]
lixiangwoaini1314 2010-02-05
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#ff9900'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
LinkButton lkBtn = (LinkButton)e.Row.FindControl("LinkButton1");
lkBtn.Attributes.Add("onclick", "return confirm('您确认要删除吗?')");
}
}


这个是用简单,有很好的效果,不妨试试
别递烟哥不会 2010-02-05
  • 打赏
  • 举报
回复
哦,是RowDataBound`
别递烟哥不会 2010-02-05
  • 打赏
  • 举报
回复
把数据库里3列都绑到gridview隐藏Type,在DataBound的时候判断Type是否为A,进而改变颜色
puzhichen 2010-02-05
  • 打赏
  • 举报
回复
貌似有人已经说了答案了!
空白画映 2010-02-04
  • 打赏
  • 举报
回复
GridView 里面 公司名字字段用的是 HyperLinkField 请问怎么绑定?

在.cs中写一个函数
public string RedFont(object obj)
{
if(Convert.ToInt16(obj)>10000)
return " <span sytle='color:red'>"+obj.ToString()+" </span>";
else
return obj.ToString();
}

前台页面绑定时这么写
<%#RedFont(Eval("公司名称字段"))%>

<%#RedFont(Eval("公司名称字段"))%>不知道绑到哪里!
chen_ya_ping 2010-02-04
  • 打赏
  • 举报
回复
上面给出的方法我想都是可以的。楼主都可以直接用了。
criedshy 2010-02-04
  • 打赏
  • 举报
回复
 protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells["Type"].Text=="A")
{
e.Row.BackColor=Color.Red;
}
}
}
zhujiazhi 2010-02-04
  • 打赏
  • 举报
回复
邦不邦定不要紧的,关键是看你有没有取出来的
比如
select Name Contact Type from table

返回三列的值,但邦定的时候也许只邦定了前面两列,也可以获取type来判断类型的

void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//throw new Exception("The method or operation is not implemented.");
if(e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView row = e.Row.DataItem as DataRowView;
string type = row["type"].ToString();
//根据type进行判断
}
}
jenny0810 2010-02-04
  • 打赏
  • 举报
回复
没有绑定不会 帮顶

你可以绑定上type不让显示
lxiron 2010-02-04
  • 打赏
  • 举报
回复
这个应该可以搞定[Quote=引用 3 楼 criedshy 的回复:]
C# codeprotectedvoid GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{if (e.Row.RowType== DataControlRowType.DataRow)
{if(e.Row.Cells["Type"].Text=="A")
?-
[/Quote]
qq5865496 2010-02-04
  • 打赏
  • 举报
回复
DataRowView dr= ds.Tables[0].DefaultView[i];
if (dr[""].ToString.Equals(""))
{
GridView1.Rows[i].Cells[2].BackColor = System.Drawing.Color.Red;
}

在 Page_Load 里面判断
f050304004 2010-02-04
  • 打赏
  • 举报
回复
循环gridview所有行。如果某行的Type单元格满足你的条件
就将这行的样式改变即可
空白画映 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 junjie94bei 的回复:]
谢谢5楼, 我现在把Type给隐藏了  但是我 公司名字 用的是 GridView 里面 公司名字字段用的是  HyperLinkField


在.cs中写一个函数
public string RedFont(object obj)
{
    if(Convert.Tostring(obj) == "A")
      return " <span sytle='color:red'>"+obj.ToString()+" </span>";
    else
      return obj.ToString();
}

前台页面绑定时这么写
<%#RedFont(Eval("公司名称字段"))%>


我想知道 HyperLinkField 怎么绑定 <%#RedFont(Eval("公司名称字段"))%>

下面代码是GridView里 HyperLinkField的代码 <asp:HyperLinkField DataNavigateUrlFields="cataid,newsid"
                  DataNavigateUrlFormatString="view.aspx?cataid={0}&newsid={1}"
                  DataTextField=' <%#RedFont(Eval("comname"))%>' HeaderText="公司名称">
                  <ItemStyle Width="200px" />
              </asp:HyperLinkField>
[/Quote]


红色字的那块写错了 原来的是 DataTextFiled="comname"


请问怎该怎么弄啊让符合条件的 变成红色
wosizy 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 junjie94bei 的回复:]
谢谢5楼, 我现在把Type给隐藏了  但是我 公司名字 用的是 GridView 里面 公司名字字段用的是  HyperLinkField


在.cs中写一个函数
public string RedFont(object obj)
{
    if(Convert.Tostring(obj) == "A")
      return " <span sytle='color:red'>"+obj.ToString()+" </span>";
    else
      return obj.ToString();
}

前台页面绑定时这么写
<%#RedFont(Eval("公司名称字段"))%>


我想知道 HyperLinkField 怎么绑定 <%#RedFont(Eval("公司名称字段"))%>

下面代码是GridView里 HyperLinkField的代码 <asp:HyperLinkField DataNavigateUrlFields="cataid,newsid"
                  DataNavigateUrlFormatString="view.aspx?cataid={0}&newsid={1}"
                  DataTextField=' <%#RedFont(Eval("comname"))%>' HeaderText="公司名称">
                  <ItemStyle Width="200px" />
              </asp:HyperLinkField>
[/Quote]

参考此贴 HyperLinkField 怎么绑定 <%#RedFont(Eval("公司名称字段"))%>
http://topic.csdn.net/u/20090810/10/e53e1424-ad2a-477b-a807-ceddd5859103.html
空白画映 2010-02-04
  • 打赏
  • 举报
回复
谢谢5楼, 我现在把Type给隐藏了 但是我 公司名字 用的是 GridView 里面 公司名字字段用的是 HyperLinkField


在.cs中写一个函数
public string RedFont(object obj)
{
if(Convert.Tostring(obj) == "A")
return " <span sytle='color:red'>"+obj.ToString()+" </span>";
else
return obj.ToString();
}

前台页面绑定时这么写
<%#RedFont(Eval("公司名称字段"))%>


我想知道 HyperLinkField 怎么绑定 <%#RedFont(Eval("公司名称字段"))%>

下面代码是GridView里 HyperLinkField的代码 <asp:HyperLinkField DataNavigateUrlFields="cataid,newsid"
DataNavigateUrlFormatString="view.aspx?cataid={0}&newsid={1}"
DataTextField='<%#RedFont(Eval("comname"))%>' HeaderText="公司名称">
<ItemStyle Width="200px" />
</asp:HyperLinkField>
wuyq11 2010-02-04
  • 打赏
  • 举报
回复
Type值使用隐藏列
protected void GridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].Visible = false;
e.Row.Cells[2].Visible = false;
}
}
取值:
GridView.Rows [i].Cells [1].Text


<asp:HiddenField ID="Hd_SH" Value=' <%#DataBinder.Eval(Container.DataItem,"BH")%>' runat="server" />

<asp:BoundField DataField="BH" HeaderText="">
<ControlStyle CssClass="hidden" />
<FooterStyle CssClass="hidden"/>
<HeaderStyle CssClass="hidden"/>
<ItemStyle CssClass="hidden"/>
</asp:BoundField>
.hidden
{
display:none;
}
遍历判断查询值设置颜色
(int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
DataRowView dr= ds.Tables[0].DefaultView[i];
if (dr[""].ToString.Equals(""))
{
GridView1.Rows[i].Cells[2].BackColor = System.Drawing.Color.Red;
}
}

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧