zoukankan      html  css  js  c++  java
  • GridView空数据时显示表头

    方法一

    在数据绑定之前,判断查询出来的数据源是否为空,如果为空,则执行添加表头的操作。

    如:    

      if (dt.rows.Count == 0)
      {
        ShowEmptyData(dt);
      }

            private void ShowEmptyData(DataTable dt)
            {
                DataTable dtnull = new DataTable();
                dtnull = dt.Clone();//复制一个空的的表,结构如同传递过来的datatable一样
                dtnull.Rows.Add(dtnull.NewRow());//添加一个空行
                grvCustomer.DataSource = dtnull.DefaultView;
                grvCustomer.DataBind();

                grvCustomer.Rows[0].Cells.Clear();
                grvCustomer.Rows[0].Cells.Add(new TableCell());
                grvCustomer.Rows[0].Cells[0].ColumnSpan = dtnull.Columns.Count;
                grvCustomer.Rows[0].Cells[0].Text = "没有相关记录";
                grvCustomer.Rows[0].Cells[0].Style.Add("text-align", "center");

            }

    方法二

    利用gridview本身自带的空数据模板EmptyDataTemplate

    ...

    <Columns>

    </Columns>

            <EmptyDataTemplate>
              <table cellpadding="0" cellspacing="0" style="100%; text-align:center">
                    <tr style=" font-weight:bold; background-color:#f7fafe;">
                    <td style=" border-right:1px solid #c0c0c0">columns1</td>
                    <td style=" border-right:1px solid #c0c0c0">columns2</td>
                    <td style=" border-right:1px solid #c0c0c0">columns3</td>
                    <td style=" border-right:1px solid #c0c0c0">columns4</td>
                    <td style=" border-right:1px solid #c0c0c0">columns5</td>
                    </tr>
                    <tr>
                        <td colspan="5" style=" height:20px; border-top:1px solid #c0c0c0">
                            没有相关记录
                        </td>
                    </tr>
                </table>
            </EmptyDataTemplate>

     

  • 相关阅读:
    HTML5是否已为实际应用做好准备? 狼人:
    10月Web服务器调查:Apache下降 Ngnix攀升 狼人:
    微软IE9通过97.7%的CSS 2.1测试 狼人:
    PHP将死。何以为继? 狼人:
    Firefox 4.0 Beta 8开始开发 新引擎依然没影 狼人:
    Adobe发布Air 2.5支持RIM平板电脑 狼人:
    Firefox 4.0:我们2011年再见面吧 狼人:
    Adobe AIR登陆Android 狼人:
    Google不推荐在URL里使用竖线 狼人:
    40个很不错的CSS技术 狼人:
  • 原文地址:https://www.cnblogs.com/ilove35/p/2151535.html
Copyright © 2011-2022 走看看