Link to home
Start Free TrialLog in
Avatar of JeffDrummond
JeffDrummond

asked on

Turning a Datagrid DataItem into an array.

I have used this method to create an array of items from a datagrid row:

        Dim dg As DataGrid = CType(Page.FindControl("dgCharges"), DataGrid)
        Dim rowView As DataRowView = CType(dg.SelectedItem.DataItem, DataRowView)
        If Not rowView Is Nothing Then
            Dim itemArray As Array = rowView.Row.ItemArray

When running the code, the datagrid and it's selectedItem are found
but the DataItem is Nothing.  Why is this?  

If this won't work, how can I create an array of the cells from the DataGridItem?

Thanks.
Avatar of boulder_bum
boulder_bum

I believe the way to do this is to get the DataKeys from the grid item, then search your DataSource for the proper row (assuming you've set up the primary key stuff properly):

Dim keyID As Integer = CType( MyDataGrid.DataKeys(dg.SelectedItem.ItemIndex), Integer )
myDataTable.Rows.Find( 'use key to find row

As to why dg.SelectedItem.DataItem is null, I vaguely remember something about it only being available during binding, but I have about 10% certainty on that memory, so don't take my word for it.
ASKER CERTIFIED SOLUTION
Avatar of boulder_bum
boulder_bum

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JeffDrummond

ASKER

The ArrayList works well for my purposes.  Thanks!
And you may be right too about the DataItem only being available during databinding.
That's the only other time I've used it.