Custom datatable with metadata used to bind to Gridview?

I am 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 special custom attribute. In this case, the column can represent:

  • a textbox (in this case I am just showing the text from the textbox in the gridview),
  • (in this case I show "Checked" or "Unchecked" depending on whether the value of the underlying column is 1 or 0)
  • a radio button (in this case, I show "On" or "Off" depending on whether the value of the underlying column is 1 or 0).

The problem is that the column datatypes are all rows in the untyped grid-bound datasheet at this time. And the binding uses autoGenerateColumns. So:

  • How do I place a column as a radio, textbox or checkbox?
  • How do I access this flag when snapping to a grid to show the text "Checked / Unchecked" or "On / Off"?

I hope I asked this correctly. Phew!

+1


source to share


3 answers


The best way to do this is to turn off autogeneration of columns and then replace the BoundFields with the columns you like with TemplateFields. There is an ASP.NET tutorial that can fill in more details.



0


source


Good. I added the extended properties to the DataTable columns with the appropriate data type and checked them in the GridView RowDataBound event in a loop of 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.

+2


source


I'm fine with templates; I don't know how to add metadata to my data columns that I can read during the GridView data binding. For example. when I bind to the checkbox data column, I want to put the word "Checked" or "Unchecked" in the column, but I need to annotate my data table somehow so that it contains the data type of the column.

0


source







All Articles