Binding a DataView to a DataGridView

melv

New member
Joined
Sep 22, 2005
Messages
1
Location
Manchester, UK
Programming Experience
Beginner
Can someone please see the below code and spot the problem? I get the following error.
<- this is where i get a error (Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataView'.)

VB.NET:
Dim[SIZE=2] gridPoint [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Point = grdClients.PointToClient(Windows.Forms.Cursor.Position)[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] hti [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataGridView.HitTestInfo = grdClients.HitTest(gridPoint.X, gridPoint.Y)[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] hti.Type = DataGrid.HitTestType.Cell [/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] hti.Type = DataGrid.HitTestType.RowHeader [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] dtRow [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = hti.RowY[/SIZE]
[SIZE=2][COLOR=#0000ff]    Dim[/COLOR][/SIZE][SIZE=2] dv [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataView = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].grdClients.DataSource, DataView)[/SIZE]
    [SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] ThisFieldID [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](dv(dtRow).Item([/SIZE][SIZE=2][COLOR=#800000]"id"[/COLOR][/SIZE][SIZE=2]), [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]    MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"Row ID ("[/COLOR][/SIZE][SIZE=2] & ThisFieldID.ToString & [/SIZE][SIZE=2][COLOR=#800000]")"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]

Than you.
 
Last edited by a moderator:
If you have assigned a DataTable to the grid's DataSource property in the first place then you can't get it back and cast it as a DataView. Either it's a DataTable or it's a DataView. If you want the DataView then you should do this:
VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] dv [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] DataView = [/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].grdClients.DataSource, DataTable).DefaultView[/SIZE]
Having said that, you shouldn't bind a DataTable or a DataView. You should bind your DataTable to a BindingSource and then your BindingSource to the grid. You can then cast the DataSource as a BindingSource, index its Item property to get an Object reference that you can cast as a DataRowView, from which you can get a field value by indexing its Item property by column name.

Can I also point out that the DataGridView.HitTestInfo.Type property is NOT type DataGrid.HitTestType. That is for the DataGrid class, NOT the DataGridView class. You should be using the DataGridViewHitTestType enumeration.
 
Back
Top