Solved

Turning a Datagrid DataItem into an array.

Posted on 2004-09-24
4
569 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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