c# - Paggeable Datagrid ItemSource throws null exception -


i want create generic datagrid can paginate. when call constructor of datagrid_uc , pass 20 employees stores 20 employees in allobject observable collection. , filter out currentpageitems 5. upon setting datagrid.itemssource = currentpageitems throws null exception currentpageitems contain 5 items.

datagrid_uc.xaml

 public partial class datagrid_uc : usercontrol     {         private observablecollection<object> _currentpageitems;         public observablecollection<object> currentpageitems         {                         {                 return _currentpageitems;             }             private set             {                 if (_currentpageitems != value)                 {                     _currentpageitems = value;                 }             }         }          // default entries per page number         private int _pagesize = 5;         public int pagesize         {                         {                 return _pagesize;             }             set             {                 if (_pagesize != value)                 {                     _pagesize = value;                     reset();                 }             }         }          public int totalpagesnumber         {                         {                 if (allobjects != null && pagesize > 0)                 {                     return (allobjects.count - 1) / pagesize + 1;                 }                 return 0;             }         }          private int _currentpagenumber = 1;         public int currentpagenumber         {                         {                 return _currentpagenumber;             }              protected set             {                 if (_currentpagenumber != value)                 {                     _currentpagenumber = value;                  }             }         }          protected observablecollection<object> allobjects { get; set; }          public datagrid_uc()         {             initializecomponent();             datagrid.itemssource = currentpageitems;         }          public datagrid_uc(ienumerable<object> allobjects, int? entriesperpage = null)         {             allobjects = new observablecollection<object>(allobjects);              if (entriesperpage != null)                 pagesize = (int)entriesperpage;              setcurrentpageitems();         }          #region public methods          public void gotonextpage()         {             if (currentpagenumber != totalpagesnumber)             {                 currentpagenumber++;                 setcurrentpageitems();             }         }          public void gotopreviouspage()         {             if (currentpagenumber > 1)             {                 currentpagenumber--;                 setcurrentpageitems();             }         }              #endregion          #region private methods          public void setcurrentpageitems()         {             int upperlimit = currentpagenumber * pagesize;              currentpageitems =                 new observablecollection<object>(                     allobjects.where(x => allobjects.indexof(x) > upperlimit - (pagesize + 1) && allobjects.indexof(x) < upperlimit));              datagrid.itemssource = currentpageitems;         }          private void reset()         {             currentpagenumber = 1;             setcurrentpageitems();         }          #endregion          private void next_click(object sender, routedeventargs e)         {             if (currentpagenumber != totalpagesnumber)             {                 currentpagenumber++;                 setcurrentpageitems();             }         }          private void previous_click(object sender, routedeventargs e)         {             if (currentpagenumber > 1)             {                 currentpagenumber--;                 setcurrentpageitems();             }         }        } 

main window.xaml

 public partial class mainwindow : window     {         public mainwindow()         {             initializecomponent();              list<employee> emp = new list<employee>();             (int = 0; < 20; i++)             {                 employee e = new employee();                 e.id = i;                 e.name = "test " + i;                 emp.add(e);             }              datagrid_uc d = new datagrid_uc(emp, 5);             newcontentcontrol.content = d;         }     } 

the datagrid member null because second constructor missing initializecomponent call, (among other things) initializes class members defined in xaml x:name.

so change constructor this:

public datagrid_uc(ienumerable<object> allobjects, int? entriesperpage = null) {     initializecomponent();     allobjects = new observablecollection<object>(allobjects);     ... } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -