|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionFairly often the such data presentation model is used: the array of objects is bound to the <asp:GridView id="gv" runat="server" AutoGenerateColumns="False" ...
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
...
</Columns>
</asp:GridView>
and elsewhere in Page_Load: if (!IsPostBack)
{
gv.DataSource = someArray;
gv.DataBind();
}
A pretty good model, in my opinion. But the The code overviewI inherit my control from the this.PageIndexChanging += new GridViewPageEventHandler(BaseGridView_PageIndexChanging);
this.Sorting += new GridViewSortEventHandler(BaseGridView_Sorting);
The paging implementation is trivial. void BaseGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.PageIndex = e.NewPageIndex;
BindDataSource();
}
The sorting one is rather complicated. The void BaseGridView_Sorting(object sender, GridViewSortEventArgs e)
{
//for unknown reason e.SortExpression always is Ascending
if (PreviousSortExpression == e.SortExpression)
{
e.SortDirection = SortDirection.Descending;
PreviousSortExpression = null;
}
else
PreviousSortExpression = e.SortExpression;
CurrentSortExpression = e.SortExpression;
CurrentSortDirection = e.SortDirection;
ChangeHeaders(this.HeaderRow);
BindDataSource();
}
A little someting is lacking. There is none visual presentation whether grid is sorted and how it is sorted. So I change sorted column header to show this information. I do it by the means of two Unicode symbols: the up arrow (\u25bc) and the down arrow (\u25b2). private void ChangeHeaders(GridViewRow headerRow)
{
for (int i = 0; i < headerRow.Cells.Count; i++)
{
if (headerRow.Cells[i] is DataControlFieldCell)
{
DataControlField field = ((DataControlFieldCell)headerRow.Cells[i]).ContainingField;
//remove all previous sorting marks if they exist
Regex r = new Regex(@"\s(\u25bc|\u25b2)");
field.HeaderText = r.Replace(field.HeaderText, "");
if (field.SortExpression != null && field.SortExpression == CurrentSortExpression)
{
//add current sorting state mark
if (CurrentSortDirection == SortDirection.Ascending)
field.HeaderText += " \u25b2";
else
field.HeaderText += " \u25bc";
}
}
}
}
Now about filling of the grid with data. public delegate void DataSourceRequestedEventHandler(out Array dataSource);
public event DataSourceRequestedEventHandler DataSourceRequested;
Refilling of the grid occurs when user sorts grid or changes the current page of the data.
For initial filling the public void BindDataSource()
{
Array dataSource = null;
//request for the data source
if (DataSourceRequested != null)
DataSourceRequested(out dataSource);
if (dataSource == null)
throw new Exception("Failed to get data source.");
//sort the data in case of need
if (CurrentSortExpression != null)
{
ArrayList ar = new ArrayList(dataSource);
ar.Sort(new ArrayComparer(CurrentSortExpression, CurrentSortDirection));
dataSource = ar.ToArray();
}
base.DataSource = dataSource;
base.DataBind();
}
How to use itFirst of all, because I hide all paging and sorting functionality inside the new control I have to reject the old way to bind data explicitly.
So I add the event handler for the all requests for the data source and the method to call data binding. And also it is need to allow paging and soring in the <cc:GridViewEx ID="gv" runat="server" AllowPaging=True AllowSorting=True
OnDataSourceRequested="gv_DataSourceRequested" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
...
</Columns>>
That is all that you need to use this control, no additional work for paging or sorting use is required.
|
||||||||||||||||||||||