RobLog

Web Design in the World of .NET (C# and VB.NET, XML, and Javascript). I learned how to program from TheDailyWTF.com!
posts - 140, comments - 127, trackbacks - 5

My Links

News

Main Site Cert Corner Goals About Me

Article Categories

Archives

Post Categories

Image Galleries

.NET

Personal

WOW


Overcoming DataGrid Problems when Sorting on programmatically adding columns

This is fun.  It took me a LONG time to figure this one out today.  I did some research and I found that I was doing something incorrectly.

 

Situation:

  1. Using user controls
  2. One contains a datagrid base (which is going to become a custom control at some point). I call it the DataGridComposite.ascx.
    1. It has a basic datagrid with a select column and a delete column on it.
    2. It also has labels that hold information for sorting, sorting ascending versus descending, and filtering the data.
    3. It contains a total item count at the bottom.
    4. It also implements paging and handles the change of paging by a drop down list that posts back automatically.
    5. It has a dataview that the datagrid is bound to.
    6. Both the DataGrid and DataView are exposed, along with events that the control raises.
    7. There is a search function which sets up the RowFilter on the dataview.  The drop down list to add items to search against is publicly exposed.
    8. When filtered, there is a button that appears that allow the user to Display all items again.
    9. When the user clicks to edit something, a ShowAddEditPanelEvent is raised.  Caller takes over at that point.
    10. When the user clicks to Delete something, it switches to a panel to ask if they really want to delete item with ID (and pulls in the datakeyfield).  If they click Okay, a DeleteItemQueuedEvent(id as Decimal) is raised.  It is up to the caller to handle the delete from that point.
  3. Other controls include the datagrid composite user control on their page.
    1. They can subscribe to receive a PreBindingEvent()  from the Composite which allows them to add columns and define the dataKeyField.
    2. They can subscribe to a DataBoundEvent() from the Composite, which allows them to make changes to items in columns once bound.
    3. There are several other events they can receive.

 

Why this approach?

  1. Control RE-USE! Come on… J
  2. Paging should be handled automagically.
  3. Sorting should be handled automagically.
  4. You should be able to sort one way, then click and have it sort Descending as well.
  5. You should be able to dynamically change the amount of items shown in the datagrid at any time.  This should happen automagically as well.
  6. Less code over time.

 

 

So what was my problem?

            I couldn’t get it to hold state when I clicked on the datagrid column names to sort.  It wouldn’t even wire (fire) the event for DataGrid.SortCommand.  It would just instantly drop all the programmatic columns and sit there with the first two columns.  The dataview still registered that it was holding all of the Items.  This was a little bit annoying (okay, a lot).

 

I have a procedure called Binder(). Directly before binding I was raising the PreBindingEvent(), and directly after I would raise the DataBoundEvent().

 

In my calling control I am setting up all of the columns in my subscription to the PreBindingEvent().  I am making changes to some of the columns by subscribing to the DataBoundEvent().

 

I found that if I called Binder() in the page_load Event, I would get the sort to work, but nothing else.

 

 

Resources:

I ended up in Rick Strahl’s Weblog and read through some of the problems he was having.  I’ve met Rick in person (at DevEssentials) and he is a bad-ass when it comes to programming.

http://west-wind.com/weblog/posts/211.aspx

 

Here is the google query that took me there: http://www.google.com/search?ie=UTF-8&oe=UTF-8&sourceid=gd&q=Sort+Problem+DataGrid+Programmatically+Adding+columns

 

Solution:

I finally figured out that I was trying to wait until my Binder() to call the PreBindingEvent().  By then it was too late and that is why the SortCommand Event never fires.  Moving the PreBindingEvent() to the Page_Init() Event solved all problems with sorting and everything else works great.  I am thinking about renaming the PreBindingEvent() to DataGridSetUpEvent() or something to better describe its function.  Good thing this is a one time deal.  I will be able to re-use this control twice in the current project and many more times to come in the future.

 

Keep on me if you want to see a copy of this one in action.  I don’t have it up anywhere right now, but I will get a demonstration of how it works up soon if enough people want it.

posted on Thursday, May 05, 2005 11:50 PM

Feedback

# Rob Reynolds DataGrid

5/6/2005 5:50 PM | Tim Hibbard

# re: Overcoming DataGrid Problems when Sorting on programmatically adding columns

I have a similar problem, but can’t quite figure out how to work around it. It would be great if I could see how you did it … :-)

Thanks...
5/9/2005 1:25 PM | Lars Hjort

# re: Overcoming DataGrid Problems when Sorting on programmatically adding columns

I will have something up soon. Look for an article on this.
6/2/2005 6:47 AM | Robz

# re: Overcoming DataGrid Problems when Sorting on programmatically adding columns

http://maduras-chachas-desnudas.eghdappartamentovc.net.cn/
http://follando-en-la-combi.xagpcaseabetonei.com.cn/
http://anabel-cuello.xagpcaseabetonei.com.cn/
http://prostitutas-colombia.npddnailartk.com.cn/
http://hermosas-escort.ozepdistinguerez.org.cn/
http://erotismo0.npddnailartk.com.cn/
http://gif-besos-cachondos.kzoyapprovazionk.net.cn/
http://amosas-vajinaes-com.cfjxbocciareelemea.cn/
http://relatos-de-violacion.kzoyapprovazionk.net.cn/
http://fotos-indiscretas-de-minifaldas-de-jovenes.cfjxbocciareelemea.cn/
http://folladas-chupa-pijas.htfgvacanzaalpip.org.cn/
http://los-mejores-culos-penes-y-vajinas.kzoyapprovazionk.net.cn/
http://folladas-caseras-gratis.htfgvacanzaalpip.org.cn/
http://culos-bolivinas.zfbeshonenaim.com.cn/
http://video-de-la-licenciada-tetarelli-haciendo-toples.mkvtmatematicaabir.org.cn/
http://viejos-maduros-gay.mkvtmatematicaabir.org.cn/
http://videos-puchas-peludas-gratis.htfgvacanzaalpip.org.cn/
http://baao-de-mujeres-meando.xagpcaseabetonei.com.cn/
http://gif-besos-eroticos.kzoyapprovazionk.net.cn/
http://adolescentes-pajeandose.zfbeshonenaim.com.cn/
http://klass-lolitas.ozepdistinguerez.org.cn/
http://fotos-pilladas-despedidas.ozepdistinguerez.org.cn/
http://boobs-peliculas-atractivo.busivoliaicaram.org.cn/
12/4/2007 3:40 AM | beria

# re: Overcoming DataGrid Problems when Sorting on programmatically adding columns

http://relatos-con-profesoras.qlchabbonamenton.cn/
http://personas-lesvianas-download.qlchabbonamenton.cn/
http://solo-fotos-de-anahi-desnuda-y-su-chocho.jlaccollaudoasn.net.cn/
http://chicas-sexiss.yniltaharbenjelc.com.cn/
http://chupando-huevo-putas.qlchabbonamenton.cn/
http://minifaldas-travestis.lkdvpontenellealpn.com.cn/
http://conchitas-famosas.fvfpserviziometeorolb.org.cn/
http://asiaticas-sexis.lghgparrb.net.cn/
http://meadas-de-putas.fvfpserviziometeorolb.org.cn/
http://imagenes-de-mujeres-practicando-basquetbol.szwwtestocanzonebenhe.com.cn/
http://imagenes-mujeres-en-colaless.jlaccollaudoasn.net.cn/
http://relatos-coaitos-mojados.qlchabbonamenton.cn/
http://mujeres-enseaando-las-bragas.yniltaharbenjelc.com.cn/
http://bailando-perreo.lkdvpontenellealpn.com.cn/
http://ver-lindos-soldados-desnudos-gays.wenxartigianr.cn/
http://hotwife-rio.fvfpserviziometeorolb.org.cn/
http://wwwfotos-gratis-de-conchas-y-penes.lghgparrb.net.cn/
http://videos-porno-de-infantiles-mostrando-sus-vaginas.jlaccollaudoasn.net.cn/
http://biquini-open-reef.lkdvpontenellealpn.com.cn/
http://hot-latinas-vids.fvfpserviziometeorolb.org.cn/
12/4/2007 3:40 AM | beria

Post Comment

Title  
Name  
Url
Comment   
Protected by Clearscreen.SharpHIPEnter the code you see: