Pages

Wednesday 5 June 2013

Fixed gridview in asp.net


Gridview is one of the good controls which enables us to produce Excel like output in the webforms.You can learn more about grid view in the following link GridView Documentation
We had a scenario where we are supposed to give freeze pane like feature for Gridview. We had a lengthy Grid with some 20 or 30 columns . We had paging for about 20 records per page. So following were the requirements.
1. Grid must have a Fixed header.
2. it should be scrollable horizontally.
3. it should be scrollable vertically.
I did a lot of search and could only get solutions that worked only where the columns are limited. So I did a small R&D and derived at a solution which i will be sharing now. Take for example a Timesheet application and it has a Grid. We are going to give fixed header feature for this Grid.

namespace CustomControl

{

[
ToolboxData("<{0}:ExGridView runat=\"server\"> </{0}:ExGridView>")


]public class ExGridView : GridView

{public ExGridView() { }

public Unit GridHeight { get; set; }

private String CalculateWidth()

{string strWidth = "auto";

if (!this.Width.IsEmpty)

{

strWidth =
String.Format("{0}{1}", this.Width.Value, ((this.Width.Type == UnitType.Percentage) ? "%" : "px"));


}return strWidth;

}private String CalculateHeight()

{string strHeight = "200px";

if (!this.GridHeight.IsEmpty)

{

strHeight =
String.Format("{0}{1}", this.GridHeight.Value, ((this.GridHeight.Type == UnitType.Percentage) ? "%" : "px"));


}return strHeight;

}protected override void Render(HtmlTextWriter writer)

{//render header row

writer.Write("<table border='0' cellspacing='" + this.CellSpacing.ToString() + "' cellpadding='" + this.CellPadding.ToString() + "' style='width:" + CalculateWidth() + "'>");

GridViewRow customHeader = this.HeaderRow;

if (this.HeaderRow != null)

{

customHeader.ApplyStyle(
this.HeaderStyle);


if (AutoGenerateColumns == false)

{int i = 0;

foreach (DataControlField col in this.Columns)

{

customHeader.Cells[i].ApplyStyle(col.HeaderStyle);

i++;

}

}

customHeader.RenderControl(writer);
this.HeaderRow.Visible = false;

}

writer.Write(
"</table>");


//render data rows

writer.Write("<div id='" + ClientID + "_div' style='" +

"padding-bottom:5px;overflow-x:scroll;overflow-y:scroll;" +

"width:" + CalculateWidth() + ";" +

"height:" + CalculateHeight() + ";" +

"background-color:#FBFAEA;'>");

//get the pager row and make invisible

GridViewRow customPager = this.BottomPagerRow;

if (this.BottomPagerRow != null)

{this.BottomPagerRow.Visible = false;
}
base.Render(writer);

writer.Write("</div>");

//render pager row

if (customPager != null && this.PageCount > 0)

{
writer.Write("<table border='0' cellspacing='" + this.CellSpacing.ToString() + "' cellpadding='" + this.CellPadding.ToString() + "' style='width:" + CalculateWidth() + "'>");

customPager.ApplyStyle(this.PagerStyle);

customPager.Visible = true;

customPager.RenderControl(writer);

writer.Write(
"</table>");


}
}

}
}

No comments:

Post a Comment