Friday, March 19, 2010

Use Scrollable GridView with fixed headers in asp.net C#

Use Scrollable GridView with fixed headers in asp.net C#

For this we need to add css to headers of gridview to keep them on the top.

First of all place a Panel on the aspx page from thetoolbox. Set height to 250px and width to 250px
and scrollbars to Vertical.

Now add a gridview inside this Panel and set the datasource to populate gridview.

Now Add below mention css style in the head section of page



<head runat="server">
<title>Creating scrollable GridView with fixed headers</title>
<style type="text/css">
  .header
  {
    font-weight:bold;
    position:absolute;
    background-color:White;
  }
  </style>
</head>


 RowDataBound event of GridView.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if(e.Row.RowIndex == 0)
           e.Row.Style.Add("height","40px");
        }
    }