?
Solved

Turning a Datagrid DataItem into an array.

Posted on 2004-09-24
4
Medium Priority
?
571 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

743 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