ASP.NET : Freeze GridView Header using GridViewScroll jQuery plugin

查看效果演示,请点击这里

When you have lots of rows to be displayed in a gridview say for example 500 records, if an user is interested in seeing 400th record, the user has to scroll all the way down through the page, if there is no paging or scrolling functionality. Also when gridview is made scrollable, headers will also scroll along with the other gridview contents making it difficult for the user to understand the data properly. There are many javascript and css solutions to freeze gridview header, but each of them has its own drawback like some solutions are not compatible with all browsers, some does not get aligned properly and some does not support other gridview features such as paging and sorting. 


Recently, I came across a jQuery plugin called GridViewScroll that enables scrolling in a gridview with fixed headers and is compatible with majority of the browsers. Since this is a client side plugin, this can be used with simple html table also. I used this plugin in one of my real-time applications and found this to be very efficient, since it can be used along with other features of gridview such as paging, sorting and can freeze gridview columns too. We can also use this plugin with Gridview inside AJAX UpdatePanel for other dynamic operations.




It is very simple and easy to implement.

1. Download GridViewScroll jQuery plugin from here.

2. Add the css file, jQuery file and images from the download to your ASP.NET project.

3. In your aspx file, use the below code.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Fixed Header GridView</title>   
    <link href="Styles/GridviewScroll.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> 
    <script src="Scripts/gridviewScroll.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            gridviewScroll();
        });

        function gridviewScroll() {
            $('#<%=GridView1.ClientID%>').gridviewScroll({
                width: 660,
                height: 300,
                startHorizontal:0,
                wheelstep:10,
                barhovercolor:"#3399FF",
                barcolor: "#3399FF"
            });
        } 
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
        <div style="width:670px;margin-left:auto;margin-right:auto;">
        <h2 style="text-align:center;">Freeze/Fixed GridView Header using GridViewScroll jQuery plugin</h2>
        <p style="text-align:center;">Demo by Priya Darshini - Tutorial @ <a href="">Programmingfree</a></p>                     
            <asp:GridView ID="GridView1" runat="server" Width="660px"
                AutoGenerateColumns="false" AllowPaging="false">
            <Columns>
                <asp:BoundField DataField="Code" HeaderText="Code" />
                <asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="150px" ItemStyle-Width="150px" ItemStyle-CssClass="Wrap" />
                <asp:BoundField DataField="Continent" HeaderText="Continent" />
                <asp:BoundField DataField="Region" HeaderText="Surface Area" />
                <asp:BoundField DataField="Population" HeaderText="Population" />
                <asp:BoundField DataField="IndepYear" HeaderText="Year of Independence" />
                <asp:BoundField DataField="LocalName" HeaderText="Local Name" />
                <asp:BoundField DataField="Capital" HeaderText="Capital" />
                <asp:BoundField DataField="HeadOfState" HeaderText="Head of State" />
                <asp:BoundField DataField="SurfaceArea" HeaderText="Surface Area" />
            </Columns>
            <HeaderStyle CssClass="GridviewScrollHeader" /> 
            <RowStyle CssClass="GridviewScrollItem" /> 
            <PagerStyle CssClass="GridviewScrollPager" /> 
            </asp:GridView>
        </div>  
    </div>    
    </form>    
</body>
</html>


Do not forget to reference jQuery and jQueryUI library in aspx page. In the above code, after page had loaded we are calling gridviewscroll jquery method with all necessary parameters like width and height of gridview.

4. Write code to load gridview with data in code-behind, I have written code to fetch data from mysql database below,

public partial class GridViewScroll : System.Web.UI.Page
    {
        DataTable dt;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //Fetch data from mysql database
                MySqlConnection conn = new MySqlConnection("server=localhost;uid=root;password=xxxxx;database=world; pooling=false;");
                conn.Open();
                string cmd = "select * from country";
                MySqlDataAdapter dAdapter = new MySqlDataAdapter(cmd, conn);
                DataSet ds = new DataSet();
                dAdapter.Fill(ds);
                dt = ds.Tables[0];
                //Bind the fetched data to gridview
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            catch (MySqlException ex)
            {
                System.Console.Error.Write(ex.Message);

            }              

        }
    }


5. That is all, now build the project. You can see a beautiful gridview with fixed header, horizontal and vertical scroller. Take a look at the live demo here and start using it to create fixed header gridviews.


There are a lot of other things that you can do with GridViewScroll such as  freezing columns, header column merge, row hover, cell wrap etc. I hope this post will help you to get started with GridViewScroll.

 

 

原文地址:

http://www.programming-free.com/2013/03/freeze-gridview-header-using-jquery.html

你可能感兴趣的:(jquery,GridView,asp.net,height,width,fixed,fixed)