تبلیغات
یادداشتهای علی سالخورده حقیقی - How can I set paging in a wpf DataGrid?
 
یادداشتهای علی سالخورده حقیقی
We Provide Solutions for The Future Developement.
درباره وبلاگ


دریافت لوح تقدیر از سازمان ملل به عنوان مشاور فناوری اطلاعات سازمان در انتخابات برون مرزی سال 2004.

مدیر وبلاگ : علی سالخورده
نویسندگان
نظرسنجی
از کدامیک از آنتی ویروس های زیر استفاده میکنید و راضی هستید؟








آمار وبلاگ
  • کل بازدید :
  • بازدید امروز :
  • بازدید دیروز :
  • بازدید این ماه :
  • بازدید ماه قبل :
  • تعداد نویسندگان :
  • تعداد کل پست ها :
  • آخرین بازدید :
  • آخرین بروز رسانی :
The code project article above is quite good for getting this done with ADO tables. While for most applications, it is likely to work great, and is easy to understand, there is a more "WPF-zen-like" way to do it as well, and that would be using CollectionViews. The advantage of using a CollectionView compared to the example above is that it is a bit more general in terms of what data you're putting in your grid (not that you can't make that example more general), and it fits in well with the general WPF databinding model. It gives you a place to support common operations like sorting, grouping, etc, if you need those.

I put together a very short example of a barely working PagingCollectionView bound to the .NET 4.0 DataGrid control. While the example itself is pretty trivial, it shows you at least how to get started, as you have a proxy around the actual collection of data on which you can execute simple operations like MoveToNextPage and MoveToPreviousPage.

Here's the C# for both the Window event handling and the PagingCollectionView:

using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;

namespace GridPagingExample
{
   
public partial class MainWindow : Window
   
{
       
private readonly PagingCollectionView _cview;

       
public MainWindow()
       
{
           
InitializeComponent();
           
this._cview = new PagingCollectionView(
               
new List<object>
               
{
                   
new { Animal = "Lion", Eats = "Tiger" },
                   
new { Animal = "Tiger", Eats =  "Bear" },
                   
new { Animal = "Bear", Eats = "Oh my" },
                   
new { Animal = "Wait", Eats = "Oh my isn't an animal" },
                   
new { Animal = "Oh well", Eats = "Who is counting anyway" },
                   
new { Animal = "Need better content", Eats = "For posting on stackoverflow" }
               
},
               
2
           
);
           
this.DataContext = this._cview;
       
}

       
private void OnNextClicked(object sender, RoutedEventArgs e)
       
{
           
this._cview.MoveToNextPage();
       
}

       
private void OnPreviousClicked(object sender, RoutedEventArgs e)
       
{
           
this._cview.MoveToPreviousPage();
       
}
   
}

   
public class PagingCollectionView : CollectionView
   
{
       
private readonly IList _innerList;
       
private readonly int _itemsPerPage;

       
private int _currentPage = 1;

       
public PagingCollectionView(IList innerList, int itemsPerPage)
           
: base(innerList)
       
{
           
this._innerList = innerList;
           
this._itemsPerPage = itemsPerPage;
       
}

       
public override int Count
       
{
           
get { return this._itemsPerPage; }
       
}

       
public int CurrentPage
       
{
           
get { return this._currentPage; }
           
set
           
{
               
this._currentPage = value;
               
this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentPage"));
           
}
       
}

       
public int ItemsPerPage { get { return this._itemsPerPage; } }

       
public int PageCount
       
{
           
get
           
{
               
return (this._innerList.Count + this._itemsPerPage - 1)
                   
/ this._itemsPerPage;
           
}
       
}

       
private int EndIndex
       
{
           
get
           
{
               
var end = this._currentPage * this._itemsPerPage - 1;
               
return (end > this._innerList.Count) ? this._innerList.Count : end;
           
}
       
}

       
private int StartIndex
       
{
           
get { return (this._currentPage - 1) * this._itemsPerPage; }
       
}

       
public override object GetItemAt(int index)
       
{
           
var offset = index % (this._itemsPerPage);
           
return this._innerList[this.StartIndex + offset];
       
}

       
public void MoveToNextPage()
       
{
           
if (this._currentPage < this.PageCount)
           
{
               
this.CurrentPage += 1;
           
}
           
this.Refresh();
       
}

       
public void MoveToPreviousPage()
       
{
           
if (this._currentPage > 1)
           
{
               
this.CurrentPage -= 1;
           
}
           
this.Refresh();
       
}
   
}
}

Here's the XAML for the window:

<Window x:Class="GridPagingExample.MainWindow"
       
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
Title="MainWindow" Height="350" Width="525">
   
<Grid>
       
<Grid.RowDefinitions>
           
<RowDefinition Height="Auto"/>
           
<RowDefinition Height="*"/>
       
</Grid.RowDefinitions>
       
<StackPanel Orientation="Horizontal" Grid.Row="0">
           
<Label Grid.Row="0" Margin="2">
               
<Label.Content>
                   
<Binding Path="CurrentPage">
                       
<Binding.StringFormat>Current Page: {0}</Binding.StringFormat>
                   
</Binding>
               
</Label.Content>
           
</Label>
           
<Button Content="Next" Click="OnNextClicked" Margin="2"/>
           
<Button Content="Previous" Click="OnPreviousClicked" Margin="2"/>
       
</StackPanel>
       
<DataGrid ItemsSource="{Binding}" Grid.Row="1">
           
<DataGrid.Columns>
               
<DataGridTextColumn Header="Animal" Width="*" Binding="{Binding Animal}"/>
               
<DataGridTextColumn Header="Eats" Width="*" Binding="{Binding Eats}"/>
           
</DataGrid.Columns>
       
</DataGrid>
   
</Grid>
</Window>

You could build out this CollectionView to support more functionality, some trivial, like MoveToLastPage and MoveToFirstPage, and some that will take some more thought about how you want it to behave, such as sorting. Hope it is helpful. 





نوع مطلب : MVVP، WPF، #C، 
برچسب ها : wpf، c#، Datagrid Paging، PagingCollectionView،
لینک های مرتبط :

       نظرات
1391/01/15
علی سالخورده
1397/01/26 14:00
Noᴡ I ɑm going to ԁo my breakfast, afterward һaving my breakfast coming օver ɑgain to read fuгther news.
1397/01/24 03:04
Pretty sеction of content. I ϳust stumbled upоn your web site and іn accession capital t᧐ assert tһat
I acquire actualⅼy enjoyed account your blog posts.
Аnyway I ᴡill be subscribing tо your augment and eᴠen I achievement
you access consistently rapidly.
1397/01/23 11:26
Thanks in support οf sharing sսch ɑ fastidious opinion, article іѕ fastidious,
thаts why i һave read it fսlly
1397/01/10 13:21
Hi! Ι know this іs kinda off topic but I wаs wondering if
you knew where I coᥙld get a captcha plugin fοr my comment foгm?
I'm using the same blog platform аs yoսrs ɑnd
I'm having problems finding one? Thanks а ⅼot!
1397/01/7 13:32
If you ɑre goіng for best cօntents lіke
me, only pay a visit this site daily since it gives feature сontents,thanks
1396/10/7 16:40
http://tubitv.wikidot.com
1396/05/15 23:26
Wonderful article! This is the type of info that are meant to
be shared around the internet. Shame on Google for no longer positioning this submit upper!
Come on over and talk over with my web site . Thank you =)
1396/05/15 16:19
I was curious if you ever thought of changing
the structure of your site? Its very well written; I love what youve got to say.

But maybe you could a little more in the way of content so
people could connect with it better. Youve got an awful lot of text
for only having 1 or 2 images. Maybe you could space it
out better?
1396/05/14 22:51
First off I would like to say fantastic blog!
I had a quick question that I'd like to ask if you don't mind.
I was interested to know how you center yourself and clear your head before
writing. I've had trouble clearing my mind in getting
my ideas out there. I do enjoy writing but it just seems like
the first 10 to 15 minutes tend to be wasted just trying to figure out how to begin. Any suggestions or tips?
Cheers!
1396/05/13 16:58
Today, I went to the beach with my children. I found a
sea shell and gave it to my 4 year old daughter
and said "You can hear the ocean if you put this to your ear." She put the shell to her ear and screamed.
There was a hermit crab inside and it pinched her ear.
She never wants to go back! LoL I know this is entirely off topic but
I had to tell someone!
1396/05/6 22:06
I visited many web pages however the audio quality for audio songs current at this
website is genuinely wonderful.
1396/04/30 22:35
I could not resist commenting. Perfectly written!
1396/03/3 08:11
Ridiculous quest there. What happened after? Good luck!
1396/01/25 04:22
I always used to study piece of writing in news papers but now as
I am a user of web so from now I am using net for articles or reviews,
thanks to web.
1396/01/22 23:51
Great article. I am going through many of these issues as well..
1396/01/16 07:22
Amazing things here. I am very happy to look your post. Thanks a
lot and I am looking ahead to contact you. Will you please drop me a mail?
 
لبخندناراحتچشمک
نیشخندبغلسوال
قلبخجالتزبان
ماچتعجبعصبانی
عینکشیطانگریه
خندهقهقههخداحافظ
سبزقهرهورا
دستگلتفکر