Solved

Turning a Datagrid DataItem into an array.

Posted on 2004-09-24
4
565 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:JeffDrummond
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:boulder_bum
Comment Utility
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.
0
 
LVL 8

Accepted Solution

by:
boulder_bum earned 500 total points
Comment Utility
And if you want to use only the DataGrid cells, you can simply have a method to get the values from the controls (or access the values directly without a method):

Private Sub GetValues() As ArrayList
     Dim al As ArrayList = New ArrayList()
   
     al.Add( CType( dg.SelectedItem.Cells(0).Controls(1), TextBox ).Text )
     al.Add...
     etc...
End Sub
0
 

Author Comment

by:JeffDrummond
Comment Utility
The ArrayList works well for my purposes.  Thanks!
0
 

Author Comment

by:JeffDrummond
Comment Utility
And you may be right too about the DataItem only being available during databinding.
That's the only other time I've used it.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now