Title: DataGrid Item Template dynamisch erzeugen

Hallo!

Ich baue ein DataGrid dynamisch auf.

Eine Spalte soll im Bearbeitungsmodus eine DropDownList beinhalten.

Das ist der Aufruf:

myTemplateColumn3.EditItemTemplate = New DropDownListColumn(myCol, myTab) 'myCol: Spaltenname, myTab: Tabellenname


Und so sieht die DropDownListColumn-Klasse aus:

Public Class DropDownListColumn
    Inherits System.Web.UI.Page
    Implements ITemplate

    Dim tableName As String
    Dim columnName As String

    Public Sub New(ByVal colName As String, ByVal tabName As String)
        Me.columnName = colName
        Me.tableName = tabName
    End Sub

    Public Sub myInstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn

        Dim myDropDownList As DropDownList = New DropDownList
        myDropDownList.DataSource = getDDLSource(tableName) 'liefert ein DataSet mit dem Inhalt der Tabelle tableName zur�ck

        myDropDownList.DataValueField = columnName
        myDropDownList.DataBind()
        myDropDownList.Items.Insert(0, New ListItem("00"))

        AddHandler myDropDownList.DataBinding, AddressOf Me.BindDropDownListColumn
        container.Controls.Add(myDropDownList)

    End Sub

    Public Sub BindDropDownListColumn(ByVal sender As Object, ByVal e As EventArgs)
        Dim ddlControl As DropDownList = CType(sender, DropDownList)
        Dim container As DataGridItem = CType(ddlControl.NamingContainer, DataGridItem)
        Dim t As String

        Try
            t = (CType(container.DataItem, DataRowView).Row(columnName)).ToString
        Catch
            t = Nothing
        End Try

        If Not t = Nothing Then
            ddlControl.SelectedValue = t 'zeigt keine Wirkung
            'ddlControl.SelectedIndex = 3 'zeigte auch keine Wirkung
        End If

    End Sub
End Class


Es klappt auch sehr gut.

Mein Problem ist nur, dass ich ein Item nicht vorselektieren kann und ich kann auch keine Items hinzuf�gen!!

Warum? Es wird anscheinend wieder �berschrieben…aber was mache ich Falsch?

Danke und Gru�

T. Poplawski

Antwort per Email an