0

I'm trying to do this in an item_databound event of a datagrid in asp.net

Dim EntType As EmployeeEntity = DirectCast(e.Item.DataItem, EmployeeEntity )

but I encounter the error

Cannot convert to class EmployeeEntity

The EmployeeEntity class has the same members as the items in e.Item.DataItem's DataRowView items. so how else do i cast the contents, without having to actually set each property of EemployeeEnity individually, from the e.Item.DataItem ?

mdm
  • 12,480
  • 5
  • 34
  • 53
  • A dataItem would be the underlying dataview of the datasource the datagrid items are being bound to. most commonly you would find this code. dim dr as datarow = Ctype( e.item.dataItem, DataRowview).row – user796558 Jun 13 '11 at 20:40
  • 1
    I realize that. What are you binding to? – SLaks Jun 13 '11 at 20:44
  • **What are you binding it to**? – SLaks Jun 13 '11 at 20:52
  • trying to bind it to a List(of EmployeeEntity). – user796558 Jun 13 '11 at 20:57
  • Then `DataItem` should be an `EmployeeEntity`. What do you see in the debugger? – SLaks Jun 13 '11 at 21:02
  • you are correct. actually the datasource is a datatable of the Employee table. (sorry about the confusion earlier). in trying to make it easier to read, i'd li – user796558 Jun 13 '11 at 21:19
  • you are correct. actually the datasource is a datatable (sorry about confusing before). in trying to make it easier to read, i'd like to cast it to Type entity to be able to access/assign the properties easily eg. if EmployeeEntity.Active then EmployeeEntity.Update()... end if. the reason i'd like to use datatable for a datasource is because objectDatasource control allows sorting with dataTables & Datasets but not with a list (of object) – user796558 Jun 13 '11 at 21:29

2 Answers2

1

You cannot magically turn one type into another, even if they have the same properties.

You need to copy the properties yourself.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

One word, AutoMapper

DDiVita
  • 4,225
  • 5
  • 63
  • 117