다음을 통해 공유


WPF DataGrid: Frozen Row Sample

It looks that this is a frequently requested feature and unfortunately didn't make the v1.  It is still possible to extend the DataGrid to do so.  A community developer has already posted a sample on creating a footer row for the DataGrid here, https://thibaud60.blogspot.com/2008/10/wpftoolkit-datagrid-with-footer-and.html.  I thought I would post a sample for creating pinned rows starting from the top.  This is similar to the excel pinning functionality. 

I won't really go into too much implementation detail but I basically follow a similar pattern to the way frozen columns are implemented.  The core of the code is in a CustomDataGridRowsPresenter.ArrangeOverride method.  I tried to make it as simple as possible so it could be easily understood and as a result of that I didn't implement virtualization.  Well, that's part of the reason I didn't implement virtualization.  VirtualizingStackPanel isn't very extendable when it comes to it's virtualization and recycling functionality.  I would have to do a good bit of my own implementation for it and it would take a good bit of time.  So, the sample only works when row and column virtualization is turned off.  If anyone feels up to making it work with virtualization, please be my guest. = ) 

Anyway, you can download the sample here.  On the left pane I added buttons to increase/decrease the frozen row and column counts. 

Note: The purpose of the sample is for learning purposes only.  Please do not use this in production code as it is not fully tested.

DataGrid_V1_FrozenRowSample.zip

Comments

  • Anonymous
    October 31, 2008
    PingBack from http://mstechnews.info/2008/10/wpf-datagrid-frozen-row-sample/

  • Anonymous
    March 12, 2009
    How to I frozen bottom to top? So I can lock the last row in the Data Grid?

  • Anonymous
    April 10, 2009
    UPDATE: the WPF DataGrid v1 has just released. For more information, see this post . The information

  • Anonymous
    February 20, 2010
    The comment has been removed

  • Anonymous
    March 02, 2011
    Hi Vincent,     I tried the code in a sample custom datagrid. It works perfectly when we set the height og datagrid to some value. If we set the height to Auto means some row gets disappear...Any idea on this?