Search code examples
gridviewdynamicbindingdatatabletyped

Custom datatable with metadata used for binding to a Gridview?


I'm binding a datatable to a gridview control, and when I format each column I need to format the data based on whether that column has a particular custom attribute. In this case, a column can represent:

  • a text box (in which case I just display the text from the textbox in the gridview),
  • a checkbox (in which case I display "Checked" or "Unchecked" based on whether the column's underlying data value is 1 or 0),
  • a radio button (in which case I display "On" or "Off" based on whether the column's underlying data value is 1 or 0).

The problem is that the column data types are all strings in the untyped data table being bound to the grid, currently. And the binding uses autoGenerateColumns. So:

  1. How do I flag a column as being either a radio, a textbox, or a checkbox?
  2. How do I access that "flag" while binding to the grid to show the text, "Checked/Unchecked", or "On/Off"?

I hope I asked this right. Phew!


Solution

  • The best way to do this is to turn off the autogeneration of columns, and then replace the BoundFields for the columns you care about with TemplateFields. There is a tutorial on ASP.NET that can fill in more of the details.