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             }

现在显示效果如下:

问题解决

原文地址:https://www.cnblogs.com/hfliyi/p/2626567.html