SmartGridView 控件EmptyDataTemplate存在问题

项目中用到SmartGridView控件,当绑定数据数据时候,如果数据为空想让它显示“没有任何数据”,有数据则直接绑定。

源代码如下:

源代码
 1   <yyc:SmartGridView runat="server" ID="huahewu" AutoGenerateColumns="false" CellPadding="0"
 2             CellSpacing="0" BorderWidth="0" align="center" AllowPaging="True" OnPageIndexChanging="huahewu_PageIndexChanging">
 3             <Columns>
 4                 <asp:TemplateField HeaderText="中文名称">
 5                     <ItemTemplate>
 6                         <a href="../Compound/CompoundBasicDetails.aspx?ID=<%#Eval("COMPOUNDID") %>" title="<%#Eval("CHINNAME") %>"
 7                             style="color:Blue; text-decoration: underline;">
 8                             <%#((System.Data.DataRowView)Container.DataItem)["CHINNAME"] %></a>
 9                     </ItemTemplate>
10                     <ItemStyle Width="25%" />
11                 </asp:TemplateField>
12                 <asp:TemplateField HeaderText="CAS号">
13                     <ItemTemplate>
14                         <%#((System.Data.DataRowView)Container.DataItem)["CAS_NUMBER"]%>
15                     </ItemTemplate>
16                     <ItemStyle Width="25%" />
17                 </asp:TemplateField>
18                 <asp:TemplateField HeaderText="分子式">
19                     <ItemTemplate>
20                         <%#((System.Data.DataRowView)Container.DataItem)["MOLFORMULA"]%>
21                     </ItemTemplate>
22                     <ItemStyle Width="25%" />
23                 </asp:TemplateField>
24                 <asp:TemplateField HeaderText="分子量">
25                     <ItemTemplate>
26                         <%#((System.Data.DataRowView)Container.DataItem)["MOLWEIGHT"]%>
27                     </ItemTemplate>
28                     <ItemStyle Width="25%" />
29                 </asp:TemplateField>
30             </Columns>
31             <PagerSettings Position="TopAndBottom" />
32             <RowStyle CssClass="tr3" Font-Size="12px" Height="28px" />
33             <HeaderStyle CssClass="itable_title" />
34             <EmptyDataTemplate>
35                 <tr class="itable_title">
36                     <td width="25%">
37                         中文名称
38                     </td>
39                     <td width="25%">
40                         CAS号
41                     </td>
42                     <td width="25%">
43                         分子式
44                     </td>
45                     <td width="25%">
46                         分子量
47                     </td>
48                 </tr>
49                 <tr class="tr3">
50                     <td colspan="4">
51                         没有任何数据
52                     </td>
53                 </tr>
54             </EmptyDataTemplate>
55         </yyc:SmartGridView>

在后台绑定数据

      DataSet ds = whBll.CheckHistory(historyModel);
        if (ds == null || ds.Tables.Count <= 0 || ds.Tables[0].Rows.Count <= 0)
        {
            DataTable dt = CreateTempTable();
            grid_histroy.DataSource = dt;
            grid_histroy.DataBind();
        }
        else
        {
            grid_histroy.DataSource = ds.Tables[0];
            grid_histroy.DataBind();
        }

运行页面后发现,页面样式存在一个问题。

用火狐调试,发现样式如下:

可以看到多了一行,如上图蓝色标出部分,

现在对代码修改:

更改后的代码
 1   dt = CreateTempTable();
 2             DataRow dr = dt.NewRow();
 3             dr["trans_id"] = "";
 4             dr["location_id"] = "";
 5             dr["trans_date"] = "";
 6             dr["contents"] = "";
 7             dr["unit"] = "";
 8             dr["trans_by"] = "";
 9             dr["trans_mode"] = "";
10             dt.Rows.Add(dr);
11             this.grid_histroy.DataSource = dt;
12             this.grid_histroy.DataBind();
13             for (int i = 0; i < this.grid_histroy.Rows.Count; i++)
14             {
15                 for (int j = 1; j < this.grid_histroy.Columns.Count; j++)
16                 {
17                     this.grid_histroy.Rows[i].Cells.RemoveAt(1);
18                 }
19                 this.grid_histroy.Rows[i].Cells[0].ColumnSpan = 7;
20                 this.grid_histroy.Rows[i].Cells[0].Text = "没有查询到历史信息";
21             }

现在显示效果如下:

问题解决

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以在GridView中添加空行,以便在没有数据时显示一些文本。可以通过以下两种方法来实现: 1. 在GridView的模板中添加一个空行 ``` <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Age" HeaderText="Age" /> </Columns> <EmptyDataTemplate> <tr> <td colspan="2">No data found.</td> </tr> </EmptyDataTemplate> </asp:GridView> ``` 在EmptyDataTemplate标记中添加一个空行,并在其中添加希望显示的文本。当GridView没有数据时,将显示此文本。 2. 在代码中添加空行 您可以在绑定数据之前检查数据源是否为空,如果是空的,则添加一个空行。例如: ``` protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataTable dt = GetDataFromDatabase(); if (dt.Rows.Count == 0) { dt.Rows.Add(dt.NewRow()); // 添加一行 GridView1.DataSource = dt; GridView1.DataBind(); int totalColumns = GridView1.Rows[0].Cells.Count; GridView1.Rows[0].Cells.Clear(); GridView1.Rows[0].Cells.Add(new TableCell()); GridView1.Rows[0].Cells[0].ColumnSpan = totalColumns; GridView1.Rows[0].Cells[0].Text = "No data found."; // 显示文本 } else { GridView1.DataSource = dt; GridView1.DataBind(); } } } ``` 在代码中添加一行,并将其绑定到GridView中。然后,使用GridView.Rows[0].Cells.Clear()方法清除第一行的单元格,并使用GridView.Rows[0].Cells.Add()方法添加一个单元格,该单元格跨越所有列,并显示您希望在空行中显示的文本。 请注意,在第二种方法中,您需要手动创建和绑定数据源,以便在没有数据时添加空行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值