如何把绑定在datagrid中的数据0,1改变显示为是、否?

netwalking 2004-11-17 09:27:27
有个DATAGRID,绑定数据源后,有以数据库的列是以“0”,“1”存储的,但显示在DATAGRID上想把“0”表示为“是”“1”表示为“否”,如何搞定!
好像是在DataBinder.Eval(Container,。。。)处理的!但忘了!
请赐教!!
在线等!请给C#源码!
...全文
162 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
valu 2004-11-17
  • 打赏
  • 举报
回复
多功能类:
<script language="C#" runat="server">
public string IIF(bool sc, string rt, string rf){
if(sc){
return rt; //返回真值
}
else{
return rf; //返回否值
}
}
</script>

调用:
<%# V6.IIF(DataBinder.Eval(Container.DataItem, "你的字段").ToString()=="0", "是", "否") %>
lufly2000 2004-11-17
  • 打赏
  • 举报
回复
方法很多:

1.在sql中直接生成:select case when 1 then '男' when 0 then '女' end from table

2.在DataGrid的ItemDataBound事件中绑定,楼上有实例。

3.在.aspx的DataGrid列中写<%DataBinder.Eval(Container.DataItem,"字段名")=="1"?"男":"女"%>

思路大概如此,具体写法去搜搜吧。
adminyao 2004-11-17
  • 打赏
  • 举报
回复
方法1 :在SQL语句中进行
方法2:
'<%# Convert.ToBoolean (DataBinder.Eval(Container.DataItem, "boo"))==true ? 是:否%>'
方法3:自已在后台写一个函数,然后返回一个值,不过与方法2没有什么区别
thooy 2004-11-17
  • 打赏
  • 举报
回复
public void DataGrid1_bind(object sender, DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{

Label label1 = (Label)e.Item.FindControl("label") //label为你在datagrid中的绑定为0,1的Label控件
if(label1.Text == "True")
{
label1.Text = "是";
}
else
{
label1.Text = "否";
}

}

}
别忘了在datagrid里,加上onItemDataBound="DataGrid1_bind"
rickjelly2004 2004-11-17
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3500/3500945.xml?temp=6.553286E-02
rickjelly2004 2004-11-17
  • 打赏
  • 举报
回复
<%# Convert.ToBoolean (DataBinder.Eval(Container.DataItem, "boo"))==1 ? 是:否%>'
showjun 2004-11-17
  • 打赏
  • 举报
回复
private void ChengedStatus()
{
foreach (DataGridItem item in dgrdQuery.Items) //dgrdQuery,你的datagrid名字
{
if(item.Cells[1].Text.ToString().Trim()=="0")
{item.Cells[1].Text="是";}
if(item.Cells[1].Text.ToString().Trim()=="1")
{item.Cells[1].Text="否";}
}
}
colinliu 2004-11-17
  • 打赏
  • 举报
回复
select case a
when a=0 then '是'
when a=1 then '否'
end as 列名1
from table1


在sql就搞定了
suntonycomm 2004-11-17
  • 打赏
  • 举报
回复
private void grdTopics_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemIndex >= 0)
{
String hint;
hint=e.Item.Cells[7].Text; //获取列内容
if (hint==1){
e.Item.Cells[7].Text='是';
}
else{
e.Item.Cells[7].Text='否';
}
}
}
pkkingofice 2004-11-17
  • 打赏
  • 举报
回复
'<%# Convert.ToBoolean (DataBinder.Eval(Container.DataItem, "boo"))==true ? 是:否%>'
GoNet 2004-11-17
  • 打赏
  • 举报
回复
在后台编写一个public方法返回是或否在datagrid中将其绑定
netwalking 2004-11-17
  • 打赏
  • 举报
回复
当然是在C#中解决!!
Jack2013tong 2004-11-17
  • 打赏
  • 举报
回复
在DataGrid 的DataGrid_ItemDataBound事件中加入下面的代码
ListItemType myType=e.Item.ItemType;
if(myType==ListItemType.Item)
{
if((e.Item.Cells[n].Controls[0]).Text.Trim()=="0")
{
e.Item.Cells[n].Controls[0]).Text="是";
}
也可以直接在SQL语句中作转换
select a,b,case your when "0" then "是" when "1" then "否" end from table
suntonycomm 2004-11-17
  • 打赏
  • 举报
回复
能否在sql 中处理呢?

select case a
when a=0 then '是'
when a=1 then '否'
end as 列名1
from table1
------------------------------------------
也可以在c#中处理。

62,046

社区成员

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

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

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

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