C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » Silverlight »

Silverlight DataGrid Paging


Posted Date: 30 Jul 2009    Resource Type: Articles    Category: Silverlight
Author: Prateek BhatnagarMember Level: Silver    
Rating: 1 out of 5Points: 30 (Rs 10)



Silverlight DataGrid Paging



Silverlight DataGrid does not have the built-in paging facility. So here is a small piece of code to implement paging in Silverlight DataGrid. For this custom paging
Calculate the number of pages based on total number of records and page size
Add those many buttons to a container like StackPanel and
On those buttons click events bind the DataGrid to desired range of records.



Define the following Properties and Variables


                public partial class Page : UserControl

{

//Property to hold the value of total records

public int TotalRow { get; set; }

//Property to hold the value of page size

public int PageSize { get; set; }

//Number of rows that the DataGrid

int totalRowsInGrid = 0;

Initialize the values


                public Page()

{

InitializeComponent();

//Initialize the page size

PageSize = 10;

//Get the desired data

user = User.Get();

//Set the TotalRow cout to the number of rows returned for data

TotalRow = user.Count;



BindGrid(1);

DoPaging(this.PageSize, TotalRow);

}


Create a method to get the desired range of data based on page number


/// <summary>

/// Returns paged data based on page number

/// </summary>

/// <param name="pageNumber"></param>

/// <returns></returns>

private List GetData(int pageNumber)

{

int Pagestart = (pageNumber - 1) * this.PageSize;

TextBlockStartPageNumber.Text = (Pagestart+1).ToString();

TextBlockEndPageNumber.Text = (Pagestart + totalRowsInGrid).ToString();

TextBlockTotalRecords.Text = TotalRow.ToString();

int i = TotalRow;

List PagedData;

//List PagedData = user.Skip((pageNumber - 1) * PageSize).Take(PageSize) as List;

//List PageedData = FullList.Skip((PageNumber - 1) * PageSize).Take(PageSize).ToList();

if ((i - ((pageNumber - 1) * this.PageSize)) >= this.PageSize)

PagedData = user.GetRange(Pagestart, this.PageSize);

else

PagedData = user.GetRange(Pagestart, (i % this.PageSize));

return PagedData;

}


Bind the DataGrid


/// <summary>

/// Bind grid to the desired range of records

/// </summary>

/// <param name="PageNumber"></param>

private void BindGrid(int PageNumber)

{

if (this.PageSize * PageNumber > this.TotalRow)

{

totalRowsInGrid = this.TotalRow % this.PageSize;

}

else

{

totalRowsInGrid = this.PageSize;

}

dataGridPaging.ItemsSource = GetData(PageNumber);

}


Write the click event for the paging buttons


/// <summary>

/// Handles the click event of buttons having page numbers

/// </summary>

/// <param name="sender"></param>

/// <param name="e">&/param<

void btn_Click(object sender, RoutedEventArgs e)

{

Button btn = sender as Button;

int iPageNumber = Convert.ToInt32(btn.Content.ToString());

BindGrid(iPageNumber);

}





Regard



Prateek





For more details, visit http://bprateek.blogspot.com/2009/07/silverlight-datagrid-paging.html



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Silverlight Datagrid Paging  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: File Handling in Isolated Storage in SilverLight
Previous Resource: Creating Image menu in Silverlight with Amazing Mouse over effect
Return to Discussion Resource Index
Post New Resource
Category: Silverlight


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use