Solved

Why can I not cast a DataGridItemEventArgs.Item.DataItem to a StringArray? Please help!

Posted on 2007-11-16
6
625 Views
Last Modified: 2010-04-21
I have been able to DirectCast this object to several of my own custom objects, however I cannot get it to cast to a String()??!! Any and all suggestion and help is greatly appreciated. I will respond quickly if you need anymore info (basically, I am trying to bind a datagrid TemplateColumn (Label) to the value of strings in a string array which is my datasource, no more, no less. I think.

 Private Sub dgPrograms_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgPrograms.ItemDataBound
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim oProgramStrings As String() = DirectCast(e.Item.DataItem, String()) ****fails on this line
            Dim lblProgramName As Label = DirectCast(e.Item.FindControl("lblProgramName"), Label)
            Dim idx As Int32 = e.Item.ItemIndex

            lblProgramName.Text = oProgramStrings(idx)

        End If
    End Sub



BTW the error message I get refers to my MyBase.OnUnload method which just iterates through an UNRELATED DataGrid's items (another DG on the page; not tied to this DG at all)- I'm not sure why the error points to this, but it does.

Thanks a bunch for helping!

PS: C# examples are welcome- I am proficient in both VB and C# (I prefer VB, but the project I am working on is all VB.NET- 1.1, hence DataGrid and not GridView..
0
Comment
Question by:OddiC
  • 3
  • 3
6 Comments
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 20301787
The dataitem represents only one chuck of the datasource, so in this case, it's a string, not a string array.  In otherwords, try casting it to a string!
0
 

Author Comment

by:OddiC
ID: 20301828
Halfway there! I got the first item in my string array to show up in the datagrid- however, the Dg stops populating after this. Basically, I have a string array (read from an xml file) which I need to iterate through and set each Dg row to its string values (there should be as many rows in the Dg as strings in the String()).

Any thoughts on why it stops after one? What can I try to let it read all of the strings?

PS: My datasource is this string array
0
 
LVL 33

Expert Comment

by:raterus
ID: 20301853
There isn't nothing you can really do to prevent the rest of the datagrid from binding, my only guess is there is only one item in this string array.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:OddiC
ID: 20301856
Never thee mind! I did some breakpointing and watching, and realized I had a brain lapse and was constructing the xml incorrectly in some other code.. You solution works perfect. Thanks a million, friend!
0
 

Author Closing Comment

by:OddiC
ID: 31409663
Exellent answer from an expert. Very staightforward, (extremely; <5 min.) prompt response.
0
 
LVL 33

Expert Comment

by:raterus
ID: 20301876
Glad to hear you got it working!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

19 Experts available now in Live!

Get 1:1 Help Now