Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Turning a Datagrid DataItem into an array.

Posted on 2004-09-24
4
Medium Priority
?
572 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:boulder_bum
ID: 12145188
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 2000 total points
ID: 12145268
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
ID: 12145513
The ArrayList works well for my purposes.  Thanks!
0
 

Author Comment

by:JeffDrummond
ID: 12145524
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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