Question

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!

Was it helpful?

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.

OTHER TIPS

Okay- I added extended properties to the DataTable columns with the proper data type and checked them on the GridView's RowDataBound event in a loop from 1..numColumns:

myProperty=e.Row.DataItem.dataview.table.columns(i).extendedproperties("TYPE")

and set the value (e.Row.Cells(i).Text) based on the type.

I'm okay with templatefields; what I don't know is how to add metadata to my datatable columns that I can read during GridView databinding. E.g. when I'm binding to a column that has checkbox data, I want to put the word "Checked" or "Unchecked" in the column, but I need to somehow annotate my data table so it contains the column data type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top