Pregunta

I'm binding a ddl inside a gridview inside an EditItemTemplate. The values bind but the index is incrementing up a step. I tried cheating a little and it helped but it revealed a second problem.

<asp:DropDownList ID="ddl_GetLists"
                    AppendDataBoundItems="true"
                    DataSourceID="sourceListData" 
                    DataValueField="PK_dn_ID" 
                    DataTextField="fld_dn_name"
                    SelectedIndex='<%#Eval("listID")-1 %>'
                    runat="server" />

I'm assuming it's being treated like an array, starting from 0 rather than 1. The second problem is that when applying the "-1" it seems to be interpreting the index as values rather than ID's.

PK      
1       Not Assigned
2       Test List One
3       Test List Two
5       Test List Three
7       Test List Four
NULL    NULL

If a record has PK of 2, it reads "Test List One", PK 3 reads "Test List Two". No problem. When the PK hits 5, it reads "Test List Four" and 7 will kick it back to 1 or "Not Assigned".

<EditItemTemplate>
                <div class="gridName">
                    <asp:TextBox ID="txtFirstName" Text='<%#Eval("firstName") %>' runat="server" Width="95" />
                </div>
                <div class="gridName">
                    <asp:TextBox ID="txtLastName" Text='<%#Eval("lastName") %>' runat="server" Width="95" />
                </div>
                <div class="gridEmail">
                    <asp:TextBox ID="txtEmail" Text='<%#Eval("email") %>' runat="server" Width="245" />
                </div>
                <div class="gridName">
                 <asp:DropDownList ID="ddl_GetLists"
                    AppendDataBoundItems="true"
                    DataSourceID="sourceListData" 
                    DataValueField="PK_dn_ID" 
                    DataTextField="fld_dn_name"
                    SelectedIndex='<%#Bind("listID")-1 %>'
                    runat="server" />
                </div>
            </EditItemTemplate>

    Protected Sub usersGrid_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)

    usersGrid.EditIndex = e.NewEditIndex
    BindData()

End Sub

Protected Sub usersGrid_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
    Dim userID As Integer = DirectCast(usersGrid.DataKeys(e.RowIndex).Value, Integer)

    usersGrid_Delete(userID)
    BindData()
End Sub

    Protected Sub BindData()
    Dim conn As New SqlConnection(connectionString)
    Dim ad As New SqlDataAdapter("MAINT_DIST_GET_USERS", conn)
    Dim ds As New DataSet()
    ad.Fill(ds)
    usersGrid.DataSource = ds
    usersGrid.DataBind()

    Dim al As New SqlDataAdapter("MAINT_DIST_GET_LISTS", conn)
    Dim dl As New DataSet()
    al.Fill(dl)
    listGrid.DataSource = dl
    listGrid.DataBind()

End Sub
¿Fue útil?

Solución

Ok, rookie mistake, sorry. Needed to use SelectedValue not SelectedIndex.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top