Thoughts from Dan Miser RSS 2.0
 Wednesday, January 31, 2007
I had a requirement to make a list of objects be displayed in some kind of a grid metaphor. I've used the DataGridView before, and even written some blog entries on extending it (check out the cool Search feature on my blog for more information). However, all of my grid usage had been limited to database-related use.

What I needed was a way to easily get filtering and sorting working for the list of objects. I found 2 links that helped me out a ton. The first one is Building a Drop-Down Filter List for a DataGridView Column Header Cell. This article is incredibly well-written and talks about the different interfaces you need to implement, and provides a sample, too. For the sorting, I used Brian Noyes' BindingListView class from Chapter 9 of his excellent book, Data Binding with Windows Forms 2.0.

When stitching these 2 pieces together, I noticed several things:

  • The DataGridViewAutoFilterColumnHeaderCell.UpdateFilter method was adding square brackets around the property names for some reason. I can't tell why this was being done, but my guess is it had to do with spaces in database column names. Since I'm not using it for that purpose, I removed the brackets. The other approach would have been to adjust the BindingList.UpdateFilter method to deal with the brackets, so I may revisit this in the future.
  • The BindingListView.UpdateFilter needed some minor modifications. I added a precondition guard check to make sure m_FilterString is not null. It looks like the DataGridViewAutoFilterColumnHeaderCell class will call back to the BindingListView class more frequently than Brian had anticipated.
  • I adjusted the setter of the IBindingListView.Filter property to remove the filter if it gets set to String.Empty or null.

    string IBindingListView.Filter

    {

        get

        {

            return m_FilterString;

        }

        set

        {

            if (string.IsNullOrEmpty(value))

                ((IBindingListView)this).RemoveFilter();

            else

            {

                m_FilterString = value;

                m_Filtered = true;

                UpdateFilter();

            }

        }

    }

  • You need to build up your collection of objects using BindingListView
  • You must use a BindingSource to link between your collection and the DataGridView by setting dataGridView1.DataSource = myBindingSource.

By doing all of this, I got things working the way I wanted. Now it's time to tackle the next task of getting child objects bound properly in a DataGridView.

Wednesday, January 31, 2007 2:48:22 PM (Central Standard Time, UTC-06:00)  #    Comments [1] -
.NET
Tracked by:
http://9nx-information.info/90351621/map-of-rottnest-and-bald-island.html [Pingback]
http://9np-information.info/03482839/hotels-in-imperial-city-ca.html [Pingback]
http://9nb-information.info/24003836/index.html [Pingback]
http://9ni-information.info/09140968/index.html [Pingback]
http://9ns-information.info/32818357/index.html [Pingback]
http://9ns-information.info/48513696/index.html [Pingback]
http://9ns-information.info/08177174/index.html [Pingback]
http://9nm-information.info/53861129/square-miles-of-sapelo-island.html [Pingback]
http://9ny-information.info/62030248/index.html [Pingback]
http://9ni-information.info/49766958/auto-car-car-com-cylinder-future-html-new-y... [Pingback]
http://9nl-information.info/74257270/index.html [Pingback]
http://9nj-information.info/94364526/angus-genealogy-wood-co-wv.html [Pingback]
http://9oo-information.info/03168695/index.html [Pingback]
http://9qo-information.info/89718664/index.html [Pingback]
http://9qq-information.info/37919235/quarante.html [Pingback]
http://9qq-information.info/78913525/index.html [Pingback]
http://9qh-information.info/84454083/index.html [Pingback]
http://9ol-information.info/30488587/index.html [Pingback]
http://9oo-information.info/51744174/index.html [Pingback]
http://9og-information.info/07779682/index.html [Pingback]
http://9qi-information.info/72531330/dimensionamento-gruppo-pressurizzazione.htm... [Pingback]
http://9ot-information.info/38669337/index.html [Pingback]
http://9se-information.info/31165218/index.html [Pingback]
http://9rf-information.info/51381733/management-strategies-used-for-coastal-envi... [Pingback]
http://9rs-information.info/25859106/index.html [Pingback]
http://9ro-information.info/71272637/index.html [Pingback]
http://9rb-information.info/71684793/index.html [Pingback]
http://9re-information.info/79866179/index.html [Pingback]
http://9uafm-le-informazioni.info/32580187/index.html [Pingback]
http://9uaes-le-informazioni.info/97642173/index.html [Pingback]
http://9uafg-le-informazioni.info/60806121/piana-mario-africa-settentrionale.htm... [Pingback]
http://9uaei-le-informazioni.info/19948035/index.html [Pingback]
http://9uaem-le-informazioni.info/56452847/index.html [Pingback]
http://9uafq-le-informazioni.info/84373936/index.html [Pingback]
http://9uafb-le-informazioni.info/08477875/supporto-muro-tv-lcd-42.html [Pingback]
http://9uaei-le-informazioni.info/77722132/ascolta-radio-kisskiss-napoli.html [Pingback]
http://9uagr-le-informazioni.info/12582192/berlusconi-kapo.html [Pingback]
http://9uagf-le-informazioni.info/85116148/index.html [Pingback]
http://9uagd-le-informazioni.info/64590775/index.html [Pingback]
http://9uahc-le-informazioni.info/66089456/index.html [Pingback]
http://9uaht-le-informazioni.info/55478665/index.html [Pingback]
http://9uago-le-informazioni.info/16994782/modulo-email-dreamweaver.html [Pingback]
http://9uahl-le-informazioni.info/21091979/index.html [Pingback]
http://9uahn-le-informazioni.info/90839957/ecologia-paesaggio.html [Pingback]
http://9uaht-le-informazioni.info/93952815/index.html [Pingback]
Navigation
Archive
<September 2010>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Dan Miser
Sign In
Statistics
Total Posts: 339
This Year: 5
This Month: 0
This Week: 0
Comments: 618
All Content © 2010, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)