表t1:
id name email is_lead
1 张三 (null) 1
2 李四 (null) (null)
3 王五 [email protected] (null)表里的内容已读到DataSet ds中.GridView1已绑定到ds,现在我要在Gridview1的RowDataBound事件中把表里为空(null)的换成0,我是这么做的: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow)
{
for(int i=0;i<e.Rows.Count;i++){
if(e.Rows.Cells[i].Text.toString()==""){ //判断如果单元格值为空,就重新赋值为0
e.Rows.Cells[i].Text="0";
}
}
}
}不知为什么,最后GridVies1中所有单元格变成了0,也就是说在判断e.Rows.Cells[i].Text.toString()的值时,全为了空.
打断点看的时候,e.Rows.Cells[i].Text.toString()也为空.难道e.Rows.Cells[i].Text取值不对? (以上代码是手打的,不考虑打错打漏的情况)
谢谢.
id name email is_lead
1 张三 (null) 1
2 李四 (null) (null)
3 王五 [email protected] (null)表里的内容已读到DataSet ds中.GridView1已绑定到ds,现在我要在Gridview1的RowDataBound事件中把表里为空(null)的换成0,我是这么做的: protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e){
if (e.Row.RowType == DataControlRowType.DataRow)
{
for(int i=0;i<e.Rows.Count;i++){
if(e.Rows.Cells[i].Text.toString()==""){ //判断如果单元格值为空,就重新赋值为0
e.Rows.Cells[i].Text="0";
}
}
}
}不知为什么,最后GridVies1中所有单元格变成了0,也就是说在判断e.Rows.Cells[i].Text.toString()的值时,全为了空.
打断点看的时候,e.Rows.Cells[i].Text.toString()也为空.难道e.Rows.Cells[i].Text取值不对? (以上代码是手打的,不考虑打错打漏的情况)
谢谢.
<Columns>
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<%# Eval("name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="email">
<HeaderStyle Width="60px"></HeaderStyle>
<ItemTemplate>
<%# (Eval("email").ToString()=="")?"0":Eval("email").ToString() %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="is_lead">
<HeaderStyle Width="60px"></HeaderStyle>
<ItemTemplate>
<%# (Eval("is_lead").ToString()=="")?"0":Eval("is_lead").ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
========
假如,你这一列是模板类,并且里面嵌入了其他空件如 label ,那么获取 Cell.Text 自然是空字符串你可以使用 daishengs(横舟摆渡) 的方式但是,上面的代码性能的缺点,就是会【重复 Eval】,假如数据很多,在乎性能,那么// .aspx
<ItemTemplate>
<%# FormatMyEmail(Eval("email")) %>
</ItemTemplate>
// .aspx.cs
protected string FormatMyEmail(object arg) {
if(arg == null) return "0";
string str = arg.ToString();
return str.Length == 0 ? "0" : str;
}