problem about combination gridview-dropdownlist (and selectedvalue)

B

Ben

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top