DataGrid DropDown speichert falschen Wert

  • VB.NET (ASP)

    DataGrid DropDown speichert falschen Wert

    Hallo Forum

    Ich habe folgende Gridview in meiner Website:

    HTML-Quellcode

    1. <asp:SqlDataSource ID="CustomersSource2" runat="server" ProviderName="System.Data.OleDb"
    2. SelectCommand = "select * from MITGLIEDER order by M_AUSTRITT asc, M_NACHNAME asc" />
    3. <asp:GridView ID="GridView2" CssStyle="Black12" runat="server" SelectedRowStyle-BackColor="#FFFFCC" DataSourceID="CustomersSource2" DataKeyNames="M_ID" AutoGenerateColumns="False" HorizontalAlign="Center" BorderStyle="Solid" BorderWidth="1px" HeaderStyle-CssClass="table12">
    4. <Columns>
    5. <asp:BoundField DataField="M_ID" HeaderText="ID" SortExpression="M_ID" ItemStyle-Width="75" ControlStyle-Width="95%" ReadOnly="True" visible="false" />
    6. <asp:BoundField DataField="M_NACHNAME" HeaderText="Nachanme" SortExpression="M_NACHNAME" ItemStyle-Width="250" ControlStyle-Width="95%" ReadOnly="True" />
    7. <asp:BoundField DataField="M_VORNAME" HeaderText="Vorname" SortExpression="M_VORNAME" ItemStyle-Width="250" ControlStyle-Width="95%" ReadOnly="True" />
    8. <asp:BoundField DataField="M_MAIL" HeaderText="E-Mail" SortExpression="M_MAIL" ItemStyle-Width="250" ControlStyle-Width="95%" ReadOnly="True"/>
    9. <asp:CheckBoxField DataField="M_NL" HeaderText="Newsletter" SortExpression="M_NL" ItemStyle-Width="100" ControlStyle-Width="95%" />
    10. <asp:CheckBoxField DataField="M_EHREN" HeaderText="Ehrenmitglied" SortExpression="M_EHREN" ItemStyle-Width="100" ControlStyle-Width="95%" />
    11. <asp:TemplateField HeaderText="Funktion" SortExpression="M_STATUS" ItemStyle-Width="100">
    12. <ItemTemplate>
    13. <asp:Label ID="LABEL1" runat="server" Text='<%# Eval("M_STATUS")%>' />
    14. </ItemTemplate>
    15. <EditItemTemplate>
    16. <asp:DropDownList ID="DropDownList1" AutoPostBack="true" OnRowDataBound="OnRowDataBound" OnSelectedIndexChanged="SelectedIndexChanged" runat="server" CssClass="Input" Width="100" SelectedValue='<%# Eval("M_STATUS")%>'>
    17. <asp:ListItem Value="" Text=" "></asp:ListItem>
    18. <asp:ListItem Value="Präsident" Text="Präsident"></asp:ListItem>
    19. <asp:ListItem Value="Präsidentin" Text="Präsidentin"></asp:ListItem>
    20. <asp:ListItem Value="Kassier" Text="Kassier"></asp:ListItem>
    21. <asp:ListItem Value="Kassierin" Text="Kassierin"></asp:ListItem>
    22. <asp:ListItem Value="Aktuar" Text="Aktuar"></asp:ListItem>
    23. <asp:ListItem Value="Aktuarin" Text="Aktuarin"></asp:ListItem>
    24. <asp:ListItem Value="1.Beisitzer/in" Text="1.Beisitzer/in"></asp:ListItem>
    25. <asp:ListItem Value="2.Beisitzer/in" Text="2.Beisitzer/in"></asp:ListItem>
    26. </asp:DropDownList>
    27. </EditItemTemplate>
    28. </asp:TemplateField>
    29. <asp:BoundField DataField="M_STATUS" HeaderText="Funktion" SortExpression="M_STATUS" ItemStyle-Width="250" ControlStyle-Width="95%" visible="true" />
    30. <asp:BoundField DataField="M_GEBURTSTAG" HeaderText="Geburtstag" SortExpression="M_GEBURTSTAG" ItemStyle-Width="250" ControlStyle-Width="95%" DataFormatString = "{0:dd/MM/yyyy}" ApplyFormatInEditMode="True" />
    31. <asp:BoundField DataField="M_AUSTRITT" HeaderText="Austritt per" SortExpression="M_AUSTRITT" ItemStyle-Width="250" ControlStyle-Width="95%" DataFormatString = "{0:dd/MM/yyyy}" ApplyFormatInEditMode="True" />
    32. <asp:CommandField ButtonType="button" HeaderText=" " ShowEditButton="True" ControlStyle-CssClass="Button" HeaderStyle-CssClass="Head" >
    33. </asp:CommandField>
    34. </Columns>
    35. </asp:GridView>​


    und folgenden Code Behind:

    VB.NET-Quellcode

    1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    2. CustomersSource2.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/opendirectory/OFZ.accdb")
    3. CustomersSource2.UpdateCommand = "update MITGLIEDER set M_NL = @M_NL, M_STATUS = @M_STATUS, M_EHREN = @M_EHREN, M_GEBURTSTAG = @M_GEBURTSTAG, M_AUSTRITT = @M_AUSTRITT, M_C_USER = '" + Session("USERNAME") + "', M_C_DATE = '" + Now() + "' where M_ID = @M_ID"
    4. End Sub
    5. Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    6. If e.Row.RowType = DataControlRowType.DataRow Then
    7. Dim drv As DataRowView = TryCast(e.Row.DataItem, DataRowView)
    8. Dim ddlCategories As DropDownList = TryCast(e.Row.FindControl("DropDownList1"), DropDownList)
    9. If ddlCategories IsNot Nothing Then
    10. ddlCategories.SelectedValue = drv("M_STATUS").ToString()
    11. End If
    12. End If
    13. End Sub
    14. Protected Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    15. Dim ddl As DropDownList = DirectCast(sender, DropDownList)
    16. Dim row As GridViewRow = TryCast(ddl.NamingContainer, GridViewRow)
    17. Dim ddlv As String = DirectCast(sender, DropDownList).SelectedValue
    18. row.Cells(7).Text = ddlv
    19. End Sub​


    Eigentlich klappt alles mit ausnahme des DropDown. es wird zwar der richtige wert eingestellt beim editieren, wenn ich aber den Datensatz aktualisiere speichert er immer eine -1 oder 0 ab, und dies willkürlich...
    Weiss jemand einen Rat dazu

    Gruss

    Markus
    Am lernen...