problem about combination gridview-dropdownlist (and selectedvalue)

Discussion in 'ASP .Net Datagrid Control' started by Ben, Aug 15, 2006.

  1. Ben

    Ben Guest

    Hi,

    It's about a database containing (for simplifying) three fields: 'placenr'
    (primary key), 'place' and 'color'. The records are updatable (Edit/Update
    button) in a gridview. For updating the field 'color', the selectedvalue of
    a dropdownlist must be used, containing all possible colors (red, blue ...),
    so the field 'color' is ReadOnly in the gridview.
    Everything works, but i still have 2 problems:

    1)the new color in the field is always the first element of the dropdownlist
    (here the value "choose"), whatever i choose in it.
    2)If only the field 'place' must be updated but not the field 'color'(no
    click in the dropdownlist), the original color in the field 'color' is
    nevertheless always replaced by the first value (again value "choose") in
    the dropdownlist.

    Any hints or idea for solving that?

    Thanks
    Ben

    Here part of the code-behind file:
    ----------------------------------
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load
    ...
    sConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source
    = c:\mydb.mdb"
    sql = "SELECT [color] FROM [color];"
    d = New OleDbDataAdapter(sql, sConnectionString)
    ds = New DataSet()
    x = d.Fill(ds)

    If Page.IsPostBack Then
    dindex = DropDownList1.SelectedIndex
    dvalue = DropDownList1.SelectedValue
    DropDownList1.Items.Clear()
    End If

    z = New ListItem("choose a color", "choose")
    DropDownList1.Items.Add(z)

    For i = 0 To x - 1
    kl = ds.Tables(0).Rows(i).Item(0)
    z = New ListItem(kl, kl)
    DropDownList1.Items.Add(z)
    Next
    End Sub

    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
    ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
    If dindex > 0 Then '0 = "choose"
    DropDownList1.SelectedValue = dvalue
    End If
    End Sub
    End Class

    Here part of the aspx file:
    ---------------------------
    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConflictDetection="CompareAllValues"
    ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydb.mdb"
    OldValuesParameterFormatString="original_{0}"
    ProviderName="System.Data.OleDb"
    SelectCommand="SELECT * FROM [place]"
    UpdateCommand="UPDATE [place] SET [placename] = ?, [color] = @color WHERE
    [placenr] = ?">
    <UpdateParameters>
    <asp:parameter Name="placename" Type="String" />
    <asp:ControlParameter ControlID="dropdownlist1"
    Name="color" PropertyName="SelectedValue" Type="String" />

    <asp:parameter Name="original_placenr" Type="Int32" />
    <asp:parameter Name="original_placename" Type="String" />
    <asp:parameter Name="original_color" Type="String" />
    </UpdateParameters>
    </asp:SqlDataSource>

    <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
    AutoGenerateColumns="False" CellPadding="4"
    DataKeyNames="placenr" DataSourceID="SqlDataSource1">
    <Columns>
    <asp:CommandField ShowEditButton="True"/>
    <asp:BoundField DataField="placenr" ReadOnly="True"/>
    <asp:BoundField DataField="placename" />
    <asp:BoundField DataField="color" ReadOnly="True" />
    </Columns>
    </asp:GridView>
    <asp:DropDownList ID="DropDownList1"
    runat="server" AutoPostBack="True">
    </asp:DropDownList>
     
    Ben, Aug 15, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tom Jorgenson
    Replies:
    3
    Views:
    7,031
  2. Thomas Kern
    Replies:
    1
    Views:
    412
    =?Utf-8?B?YWxhc3Bpbg==?=
    Nov 9, 2004
  3. Ben
    Replies:
    0
    Views:
    3,342
  4. Chris
    Replies:
    11
    Views:
    2,515
    Chris
    May 18, 2008
  5. Ben
    Replies:
    0
    Views:
    200
Loading...

Share This Page