OddiC
asked on
Why can I not cast a DataGridItemEventArgs.Item.DataItem to a StringArray? Please help!
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(B yVal sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles dgPrograms.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingIt em Then
Dim oProgramStrings As String() = DirectCast(e.Item.DataItem , String()) ****fails on this line
Dim lblProgramName As Label = DirectCast(e.Item.FindCont rol("lblPr ogramName" ), 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..
Private Sub dgPrograms_ItemDataBound(B
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingIt
Dim oProgramStrings As String() = DirectCast(e.Item.DataItem
Dim lblProgramName As Label = DirectCast(e.Item.FindCont
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..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
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!
ASKER
Exellent answer from an expert. Very staightforward, (extremely; <5 min.) prompt response.
Glad to hear you got it working!
ASKER
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