Freeze asp.net grid view column

后端 未结 5 470
半阙折子戏
半阙折子戏 2021-01-03 01:32

How can i freeze initial 2 -3 leftmost column in asp.net grid view? so that while horizontal scrolling initial 2 - 3 columns that got freezes will always be display.

5条回答
  •  Happy的楠姐
    2021-01-03 01:59

    Yes, it seems to be possible with some css magic, with the pinned and scrollable columns on different z-indexes to keep the pinned on top. This comes with the caveat that overflow:scroll may not be 100% portable (I've tested on IE 8/9 and Chrome FWIW).

    Take a look at this jsFiddle here

    The ASPX I used to generate the GridView is below.

    Note the css classes pinned and scrollable for fixed and scrolling columns respectively (applied to headers and items)

    But the real work is done in the css. Note especially that you need to fiddle to get your column widths correct to align the fixed td's / th's at the left.

    aspx

    css

        #scrolledGridView
        {
            overflow-x: scroll; 
            text-align: left;
            width: 400px; /* i.e. too small for all the columns */
        }
    
        .pinned
        {
            position: fixed; /* i.e. not scrolled */
            background-color: White; /* prevent the scrolled columns showing through */
            z-index: 100; /* keep the pinned on top of the scrollables */
        }
        .scrolled
        {
            position: relative;
            left: 150px; /* i.e. col1 Width + col2 width */
            overflow: hidden;
            white-space: nowrap;
            min-width: 500px; /* set your real column widths here */
        }
        .col1
        {
            left: 0px;
            width: 50px;
        }
        .col2
        {
            left: 50px; /* i.e. col1 Width */
            width: 100px;
        }
    

提交回复
热议问题