Hello, I have a problem. I want to Preselecting an Item in a Drop-Down List
inside a datagrid but I have an error. Show me "Object reference not set to
an instance of an object." in DataGrid1_ItemDataBound event in the code "Dim
currentgenre As String = CType(drv("cod_estado"), String)". I send the html
and codebehind code.
Please help me in this.
Regards,
Oswaldo
HTM
--------------------------------------------------------------------------------------------
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 224px; POSITION:
absolute; TOP: 152px" runat="server">
<Columns>
<asp:TemplateColumn>
<HeaderTemplate>
<asp:Label id=Label1 runat="server" Text="<%# Container.DataItem %>">
</asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList id=DropDownList1 runat="server" Width="120px"
DataTextField="descripcion" DataSource="<%#LlenarCombo() %>"
DataValueField="cod_estado">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
CODEBEHIN
----------------------------------------------------------------------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Introducir aquà el código de usuario para inicializar la página
Dim da1 As New SqlDataAdapter("select top 10
cod_orden,razon_social,estado from orden", SqlConnection1)
da1.Fill(ds, "Ordenes")
DataGrid1.DataSource = ds.Tables("Ordenes")
DataGrid1.DataBind()
End Sub
Public Function LlenarCombo() As DataTable
If Application("estados") Is Nothing Then
Dim da1 As New SqlDataAdapter("select cod_estado,descripcion
from estados", SqlConnection1)
da1.Fill(ds, "Estados")
Application("estados") = ds.Tables("Estados")
Return ds.Tables("Estados")
Else
Return CType(Application("estados"), DataTable)
End If
End Function
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) _
Handles DataGrid1.ItemDataBound
If ((e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType =
ListItemType.AlternatingItem) Or (e.Item.ItemType = ListItemType.Header)) Then
Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
Dim currentgenre As String = CType(drv("cod_estado"), String)
Dim ddl As New DropDownList
ddl = CType(e.Item.FindControl("DropDownList1"), DropDownList)
ddl.SelectedIndex =
ddl.Items.IndexOf(ddl.Items.FindByValue(currentgenre))
End If