Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Christopher Bennage

Our WPF book is now available!


DropDownField for GridView

One of the requirements on my current project involves the user defining what a grid of data should look like. We allow a user to create a "definition", which we persist in the database and then we construct a GridView at runtime based on the "definition". This mean that we don't know what fields (or even types of fields) the GridView has at design time. Additionally, we needed to support drop down lists in the GridView.

I searched for a good while hoping to find out how to programmatically add a drop down list to a GridView. All of the examples I found used a TemplateField and were based on the assumption that you knew you wanted a drop down list at design time. So I decided to build my own custom drop down list field. Reflector allowed me to dissect the various fields that shipped with the framework (BoundField, CheckBoxField, etc.) All of these classes derive from DataControlField. I won't go into too much more detail for the moment, but the major members that I had to implement for DataControlField were:

  • InitializeDataCell
  • OnDataBindField

InitializeDataCell determines the state of the current row in the GridView (is it edit or readonly). Then it instantiates the appropriate controls. In my case, the default DataControlFieldCell for read-only and a (you guessed it) DropDownList for edit. Additionally, it wires up the DataBinding event for the control it just added.

OnDataBindField is responsible for putting the right value in the control. In my case, we check to see whether or not we have a drop down list and if we do, then we apply some logic to populate the items in that list. An interesting point here is that you want to instantiate new ListItems for every row. If you only have one set of ListItems you will end up with odd behavior, because multiple DropDownLists will be referencing the same exact ListItems.



Comments

cslatt said:

This article in MSDN Magazine is a nice tutorial for adding new types of field controls.

http://msdn.microsoft.com/msdnmag/issues/06/01/CuttingEdge/

# September 22, 2006 10:02 AM

Christopher Bennage said:

Thanks Chris, I can't believed I missed that article.  I had the magazine in a stack next to my desk while I was researching how to do it! It's all about searching for the _right_ keywords in Google. :-)

# September 24, 2006 1:08 AM

pils01 said:

Hello Bennage,

Thank you a lot for your class, this is very helpful for me, but I still don't understand how can I initialize it (only by code, as I'm creating Web Parts):

private GridView gvPrinters = new GridView();

....

DropDownField tfPrinterRequ2 = new DropDownField();

tfPrinterRequ2.HeaderText = "Requirement";

tfPrinterRequ2.DataField = "Requirement";

tfPrinterRequ2.DropDownListId = "ddlRequirements";

tfPrinterRequ2.AutoPostBack = false;

tfPrinterRequ2.Initialize(true, gvPrinters);

It gives me a null reference on this line in the OnDataBindField function:

ListItem selected = ListToCopy.Items.FindByValue(output);

I think I don't initialize it well, but I don't know how to do... Please, help. Thank you

# October 27, 2006 3:32 AM

Christopher Bennage said:

Hi pils01, I just found your question.  I need to refresh my memory regarding the code, but I will take a look and get back to you.

# November 14, 2006 9:30 AM

About Christopher Bennage

Christopher is a software developer and consultant at Blue Spire Consulting, a company he co-founded with Rob Eisenberg in 2006. He is a Christian, a marginal musician, and an armchair philosopher. His interests include programming, liberal education, science, truth, beauty, and a number of deceased British authors (C. S. Lewis, G. K. Chesterton, and most recently Owen Barfield.) He lives in Tallahassee, FL with his wife and three children and still prefers to play as the Night Elves in WarCraft 3. Check out Devlicio.us!

Our Sponsors

Red-Gate!