如何冻结girdview的标题[gridview和列宽不固定]

时间:2017-03-22 15:06:41

标签: gridview webforms vertical-scrolling fixed-header-tables

我正在使用垂直滚动条实现Gridview冻结标题。我尝试了很多这里提出的解决方案。但问题是,

  1. Gridview位于面板内部,其宽度不固定。它的97%。
  2. 2.列没有固定的宽度。

    我尝试在GridviewScroll.js中建议的解决方案,但问题是固定宽度,并且Gridview没有根据窗口大小调整大小。

    我尝试了太多解决方案,但没有固定宽度的任何工作都没有,所以我不能在这里提出..

    以下是我现有的代码

    synchronized

    有什么建议吗?

2 个答案:

答案 0 :(得分:0)

我会使用DataTables。除了做你想做的事情(就我理解你的要求而言),它是成熟的,稳定的,并且还有更多的能力。

我创建了一个JsFiddle来演示:https://jsfiddle.net/objo18f9/3/

在小提琴中:标题是通过内容的垂直滚动来固定的;没有指定的列大小(当你向左或向右拖动中间分隔符(小提琴)时它们会调整大小;并且表被div包围,该$(document).ready(function() { $('#example').DataTable({ "scrollY": "200px", "scrollCollapse": true, "paging": true }); });被设置为页面宽度的97%(来自内存是一个Panel在ASP.Net中呈现的内容;并且打开了分页。

以下是与代码段相同的示例:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.13/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.13/css/jquery.dataTables.min.css">

<div width="97%">
  <table id="example" class="display" width="100%" cellspacing="0">
    <thead>
      <tr>
        <th>Name</th>
        <th>Position</th>
        <th>Office</th>
        <th>Age</th>
        <th>Start date</th>
        <th>Salary</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Tiger Nixon</td>
        <td>System Architect</td>
        <td>Edinburgh</td>
        <td>61</td>
        <td>2011/04/25</td>
        <td>$320,800</td>
      </tr>
      <tr>
        <td>Garrett Winters</td>
        <td>Accountant</td>
        <td>Tokyo</td>
        <td>63</td>
        <td>2011/07/25</td>
        <td>$170,750</td>
      </tr>
      <tr>
        <td>Ashton Cox</td>
        <td>Junior Technical Author</td>
        <td>San Francisco</td>
        <td>66</td>
        <td>2009/01/12</td>
        <td>$86,000</td>
      </tr>
      <tr>
        <td>Cedric Kelly</td>
        <td>Senior Javascript Developer</td>
        <td>Edinburgh</td>
        <td>22</td>
        <td>2012/03/29</td>
        <td>$433,060</td>
      </tr>
      <tr>
        <td>Airi Satou</td>
        <td>Accountant</td>
        <td>Tokyo</td>
        <td>33</td>
        <td>2008/11/28</td>
        <td>$162,700</td>
      </tr>
      <tr>
        <td>Brielle Williamson</td>
        <td>Integration Specialist</td>
        <td>New York</td>
        <td>61</td>
        <td>2012/12/02</td>
        <td>$372,000</td>
      </tr>
      <tr>
        <td>Herrod Chandler</td>
        <td>Sales Assistant</td>
        <td>San Francisco</td>
        <td>59</td>
        <td>2012/08/06</td>
        <td>$137,500</td>
      </tr>
      <tr>
        <td>Rhona Davidson</td>
        <td>Integration Specialist</td>
        <td>Tokyo</td>
        <td>55</td>
        <td>2010/10/14</td>
        <td>$327,900</td>
      </tr>
      <tr>
        <td>Colleen Hurst</td>
        <td>Javascript Developer</td>
        <td>San Francisco</td>
        <td>39</td>
        <td>2009/09/15</td>
        <td>$205,500</td>
      </tr>
      <tr>
        <td>Sonya Frost</td>
        <td>Software Engineer</td>
        <td>Edinburgh</td>
        <td>23</td>
        <td>2008/12/13</td>
        <td>$103,600</td>
      </tr>
      <tr>
        <td>Jena Gaines</td>
        <td>Office Manager</td>
        <td>London</td>
        <td>30</td>
        <td>2008/12/19</td>
        <td>$90,560</td>
      </tr>
      <tr>
        <td>Quinn Flynn</td>
        <td>Support Lead</td>
        <td>Edinburgh</td>
        <td>22</td>
        <td>2013/03/03</td>
        <td>$342,000</td>
      </tr>
      <tr>
        <td>Charde Marshall</td>
        <td>Regional Director</td>
        <td>San Francisco</td>
        <td>36</td>
        <td>2008/10/16</td>
        <td>$470,600</td>
      </tr>
      <tr>
        <td>Haley Kennedy</td>
        <td>Senior Marketing Designer</td>
        <td>London</td>
        <td>43</td>
        <td>2012/12/18</td>
        <td>$313,500</td>
      </tr>
      <tr>
        <td>Tatyana Fitzpatrick</td>
        <td>Regional Director</td>
        <td>London</td>
        <td>19</td>
        <td>2010/03/17</td>
        <td>$385,750</td>
      </tr>
      <tr>
        <td>Michael Silva</td>
        <td>Marketing Designer</td>
        <td>London</td>
        <td>66</td>
        <td>2012/11/27</td>
        <td>$198,500</td>
      </tr>
      <tr>
        <td>Paul Byrd</td>
        <td>Chief Financial Officer (CFO)</td>
        <td>New York</td>
        <td>64</td>
        <td>2010/06/09</td>
        <td>$725,000</td>
      </tr>
      <tr>
        <td>Gloria Little</td>
        <td>Systems Administrator</td>
        <td>New York</td>
        <td>59</td>
        <td>2009/04/10</td>
        <td>$237,500</td>
      </tr>
      <tr>
        <td>Bradley Greer</td>
        <td>Software Engineer</td>
        <td>London</td>
        <td>41</td>
        <td>2012/10/13</td>
        <td>$132,000</td>
      </tr>
      <tr>
        <td>Dai Rios</td>
        <td>Personnel Lead</td>
        <td>Edinburgh</td>
        <td>35</td>
        <td>2012/09/26</td>
        <td>$217,500</td>
      </tr>
    </tbody>
  </table>
</div>
<asp:GridView ID="GridView_Vehicle" ClientIDMode="static" ...

如果您告诉ASP.Net使用静态ID:$('#GridView_Vehicle').DataTable({ ... });,那么您应该可以将您的表格引用为:USE [qcsl] GO /***** Object: StoredProcedure [dbo].[UserAdvancedSearch] Script Date: 3/31/2017 3:25:28 PM *****/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: Mayank -- Create date: 2017-03-29 -- Description: -- ============================================= ALTER PROCEDURE [dbo].[UserAdvancedSearch] -- Add the parameters for the stored procedure here ( @user_name varchar(50) = NULL, @user_last_name varchar(50) = NULL, @role_id varchar(50) = NULL, @user_status varchar(50) = NULL, @query varchar(2000) = NULL output ) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SET @query = 'select * from user_main where 'IF (@user_name is not null ) SET @query = @query + ' (user_first_name like '''+@user_name+''' OR user_first_name IS NULL) 'IF (@user_last_name is not null ) SET @query = @query + 'AND (user_last_name like '''+@user_last_name+''' OR user_last_name IS NULL) 'IF (@user_status is not null ) SET @query = @query + 'AND (user_status = '+@user_status+') 'IF (@role_id is not null) SET @query = @query + ' AND (user_role_id_ref = '+@role_id+')' EXEC(@query); END

答案 1 :(得分:0)

这是针对具有中等行数的网格的解决方案(如果您有更多行,则可能需要使用较小的分页而不是滚动):

使用相同的规范和数据源创建两个GridView元素,但第二个元素具有ShowHeader =&#34; false&#34;。将第一个放在div中,样式=&#34;高度:38px;溢出:隐藏&#34; (您可能需要根据标题使用的字体调整高度)。将第二个放在div中,样式=&#34; max-height:400px;溢出:汽车&#34; (根据您想要显示的行数调整高度)。由于两个表都使用相同的数据,因此它们应该正确排列,第二个表可以滚动。如果要启用排序,请在服务器上执行共享数据源,并将第二个GridView放在UpdatePanel中,AsyncPostBackTrigger指向第一个。这是您示例的代码:

  <asp:Panel ID="panel_gridholder" runat="server"  style="position:absolute;left:10px;width:97%; min-width:1020px;margin-top:3px;margin-bottom:20px;" >
     <div style="height:38px; overflow:hidden">
        <asp:GridView ID="GridView_Vehicle_Header" runat="server" AutoGenerateColumns="False" ShowHeaderWhenEmpty="true" AllowSorting="true" OnSorting="GridView_Vehicle_Sorting" GridLines="None" CssClass="mGrid" AlternatingRowStyle-CssClass="alt">
            <Columns> </Columns>
       </asp:GridView>
    </div>
    <div style="max-height:400px; overflow:auto">
       <asp:UpdatePanel ID="UpdatePanel" runat="server" UpdateMode="Conditional">
          <ContentTemplate>
             <asp:GridView ID="GridView_Vehicle" runat="server" AutoGenerateColumns="False" ShowHeader="false" AllowSorting="true" OnSorting="GridView_Vehicle_Sorting" GridLines="None" CssClass="mGrid" AlternatingRowStyle-CssClass="alt">
                <Columns> </Columns>
             </asp:GridView>
          </ContentTemplate>
          <Triggers>
             <asp:AsyncPostBackTrigger ControlID="GridView_Vehicle_Header" />
          </Triggers>
       </asp:UpdatePanel>
    </div>
 </asp:Panel>