Click here to Skip to main content
13,197,796 members (43,565 online)
Click here to Skip to main content
Add your own
alternative version

Stats

55.3K views
1.1K downloads
18 bookmarked
Posted 15 Mar 2006

Custom Paging in datagrid using c# in ASP.NET

, 15 Mar 2006
Rate this:
Please Sign up or sign in to vote.
An article on Customizing the ASP.NET DataGrid Paging functionality

Introduction & Background

While I was working in a project written in C# and in an Arabic (my native) language as UI Language, I faced a lot of problems regarding the UI but, they were easy to fix.

But, I faced a big problem regarding the Paging in the DataGrid, the number simply changed the order (sequence) from right to left just with the dir attribute='rtl' (Right to Left) but, the Next and Previous links weren't the same, as the Arabic language is Right to Left alignment the next link was at the begging and the previous link was at the end and this is not correct in Arabic.

Further more, the client didn't like the paging style, so, I had to do something, I thought of doing a Panel for navigation for the DataGrid and associate it with it, but, it will require a lot of code and then testing and I didn't like this approach.

Finally, I found the following solution:

First, I wrote an extension class for the DataGrid and I have added couple of attributes like "Direction (Dir)" and number of sequence pages numbers that should be displayed.

afterward, I had to write a custom DataGridItem in order to control the hosted child controls. Then, I wrote CustomLinkButton to extend the LinkButton functionlity, I needed to add a space after each link, so, I made an override of the Render method, plus I wrote code to manipulate the HTML Text Direction too.

 

Using the code

Now let's look at the main class for the Customized DataGridItem,

/// <span class="code-SummaryComment"><summary>
</span>/// Here is the core code to initilize and format the output of
/// the CustomDataGridItem.
/// <span class="code-SummaryComment"></summary>
</span>protected override void CreateChildControls()
{
TableCell tc = new TableCell();
CustomLinkButton lb;


StartingIndex = ((CustomPagingGrid)this.Parent.Parent).CurrentStartingIndex;
CurrentPage = ((DataGrid)this.Parent.Parent ).CurrentPageIndex ;
PagesCount = ((DataGrid)this.Parent.Parent ).PageCount ;



tc.ColumnSpan = GetColumnsCount();


Controls.Clear();


// The Prevous Link 
if (StartingIndex-DisplayPagesCount >= 0)
{
lb = new CustomLinkButton(_Direction);
lb.Text="Previous";
lb.CommandName ="P"+ StartingIndex.ToString();
lb.Click += new EventHandler(this.Link_Click);
tc.Controls.Add(lb);
}

// the sequence numbers Links.
for(int i=StartingIndex ;i < 
  ( (StartingIndex+DisplayPagesCount > 
     PagesCount)?PagesCount-StartingIndex: 
     StartingIndex+DisplayPagesCount) ; i++)
{
lb = new CustomLinkButton (_Direction);

if (i==CurrentPage)
lb.Enabled = false;

lb.Text=(i+1).ToString();
lb.CommandName =(i).ToString();
lb.Click += new EventHandler(this.Link_Click);
tc.Controls.Add(lb);
}

// the Next Link
if (StartingIndex+DisplayPagesCount<PagesCount)
{
lb = new CustomLinkButton(_Direction);
lb.Text="Next";
lb.CommandName ="N"+ StartingIndex.ToString();
lb.Click += new EventHandler(this.Link_Click);
tc.Controls.Add(lb);
}

Controls.Add(tc);
}

As you can see that the overridden Method CreateChildControls overrides the base method and it creates the controls which in turn will be added to a TableCell control which in turn will be added to the DataGridItem.

The rest of the code it just easy, except there is a method called GetColumnsCount which I have used Reflection to obtain the Grid's Column's count, because the autoGenColumnsArray is a private field in the DataGrid class, and before the binding process you can't determine how many columns you have in the Grid, that's why I found this field has the correct number of columns but, in an Array format - Array of Columns -, I just need it's count no more.

Conclusion

Ok, what's the importance of a customized Paging for Arabic layout? Well, I have got some additional ideas. Maybe one day, you will need to have a new paging style(s) like, DropDownList with the list of pages, or you may change the style from Link to Button or even attach a TextBox along with Button for searching the results.

anyway, there are numerous ideas, just you have the concept and the sample code off course.

History

Before this article I was just a reader, and I really enjoyed it. It's great service that you Guys ( CodeProject Team ) provide.

and I hope it won't be my last article, because, I'm extremely busy.

Thank you all,

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Microsoftawy
Team Leader MOF, Kuwait
Kuwait Kuwait
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralEvent Bubbling Pin
redcane012-Apr-07 17:45
memberredcane012-Apr-07 17:45 
GeneralCustom Paging in datagrid Pin
sourav_the_don6-May-06 23:17
membersourav_the_don6-May-06 23:17 
GeneralRe: Custom Paging in datagrid Pin
magcp6-May-06 23:46
membermagcp6-May-06 23:46 
GeneralRe: Custom Paging in datagrid Pin
Bimal Giri20-Oct-06 12:09
memberBimal Giri20-Oct-06 12:09 
QuestionAttachments? Pin
El Gato30-Mar-06 7:36
memberEl Gato30-Mar-06 7:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171020.1 | Last Updated 15 Mar 2006
Article Copyright 2006 by Microsoftawy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid