DropDownList in a GridView or DetailsView template

P

P. Yanzick

Hello,

I've been playing with master/detail views as well as editing in the
gridview, and I ran across a strange problem that I am not exactly sure
where to go to try to solve.

I have 2 tables, a table of cars (pretty basic, an ID, a description, and a
Color ID) and a table of colors (Color ID, and a color description). I've
added a gridview and a detailsview as I'm playing with both and how to get
editing features to work the way I want to, and in this case I want a
dropdown box to appear with the color description, then write the ID to the
database.

Right now I am just getting the editing functionality working, however I
noticed something odd. In both the gridview if I use edit, or if I edit in
the detailsview, the color value is always off by 1, 1 higher than it should
be. Hoping to get an out of range error, I edited a record with the highest
color value, but instead of an error, it then moved to the lowest value
possible in the dropdown.

The code is below, if you are interested. I'd like it to default to what
the value actually is. Would anyone have any suggestions for me??? :)

Thanks
Paul

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:SqlDataSource ID="CarDisplayDS" runat="server" ConnectionString="<%$
ConnectionStrings:TestDBConnectionString %>"

SelectCommand="SELECT Car.CarID, Car.CarDescription, Color.ColorDescription,
Car.ColorID FROM Car INNER JOIN Color ON Car.ColorID = Color.ColorID">

</asp:SqlDataSource>

<asp:SqlDataSource ID="ColorListForDropDownDS" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"

SelectCommand="SELECT ColorID, ColorDescription FROM
Color"></asp:SqlDataSource>

<asp:SqlDataSource ID="DetailCarDisplayDS" runat="server"
ConnectionString="<%$ ConnectionStrings:TestDBConnectionString %>"

InsertCommand="INSERT INTO Car(CarDescription, ColorID) VALUES (,)"
SelectCommand="SELECT Car.CarID, Car.CarDescription, Car.ColorID,
Color.ColorDescription FROM Car INNER JOIN Color ON Car.ColorID =
Color.ColorID"

UpdateCommand="UPDATE Car SET CarDescription =, ColorID = FROM Car INNER
JOIN Color ON Car.ColorID = Color.ColorID"

FilterExpression="CarID={0}">

<FilterParameters>

<asp:ControlParameter

Name="CarID"

ControlID="GridView1"

PropertyName="SelectedValue" />

</FilterParameters>


</asp:SqlDataSource>


</div>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="White"

BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
DataKeyNames="CarID"

DataSourceID="CarDisplayDS" GridLines="Vertical"
AutoGenerateSelectButton="True" >

<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />

<Columns>

<asp:CommandField ShowEditButton="True" />

<asp:BoundField DataField="CarID" HeaderText="CarID" InsertVisible="False"
ReadOnly="True"

SortExpression="CarID" />

<asp:BoundField DataField="CarDescription" HeaderText="CarDescription"
SortExpression="Car Description" />

<asp:TemplateField HeaderText="ColorDescription" SortExpression="Color
Description">

<EditItemTemplate>

<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="ColorListForDropDownDS"

DataTextField="ColorDescription" DataValueField="ColorID" SelectedIndex='<%#
Bind("ColorID") %>'>

</asp:DropDownList>

</EditItemTemplate>

<ItemTemplate>

<asp:Label ID="Label1" runat="server" Text='<%# Bind("ColorDescription")
%>'></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField="ColorID" HeaderText="ColorID"
SortExpression="ColorID"

Visible="False" />

</Columns>

<RowStyle BackColor="#EEEEEE" ForeColor="Black" />

<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />

<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center"
/>

<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />

<AlternatingRowStyle BackColor="Gainsboro" />

</asp:GridView>

<br />

<asp:FormView ID="FormView1" runat="server" DataKeyNames="CarID"
DataSourceID="DetailCarDisplayDS"

DefaultMode="Edit">

<EditItemTemplate>

CarID:

<asp:Label ID="CarIDLabel1" runat="server" Text='<%# Eval("CarID")
%>'></asp:Label><br />

CarDescription:

<asp:TextBox ID="CarDescriptionTextBox" runat="server" Text='<%#
Bind("CarDescription") %>'></asp:TextBox><br />

ColorDescription:

<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ColorListForDropDownDS"

DataTextField="ColorDescription" DataValueField="ColorID" SelectedIndex='<%#
Eval("ColorID") %>'>

</asp:DropDownList><br />

<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update"

Text="Update"></asp:LinkButton>

<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"

Text="Cancel"></asp:LinkButton>

</EditItemTemplate>

<InsertItemTemplate>

CarDescription:

<asp:TextBox ID="CarDescriptionTextBox" runat="server" Text='<%#
Bind("CarDescription") %>'>

</asp:TextBox><br />

ColorDescription:

<asp:TextBox ID="ColorDescriptionTextBox" runat="server" Text='<%#
Bind("ColorDescription") %>'>

</asp:TextBox><br />

<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert"

Text="Insert">

</asp:LinkButton>

<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel"

Text="Cancel">

</asp:LinkButton>

</InsertItemTemplate>

<ItemTemplate>

CarID:

<asp:Label ID="CarIDLabel" runat="server" Text='<%# Eval("CarID")
%>'></asp:Label><br />

CarDescription:

<asp:Label ID="CarDescriptionLabel" runat="server" Text='<%#
Bind("CarDescription") %>'></asp:Label><br />

ColorDescription:

<asp:Label ID="ColorDescriptionLabel" runat="server" Text='<%#
Bind("ColorDescription") %>'></asp:Label><br />

<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit"

Text="Edit"></asp:LinkButton>

<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New"

Text="New"></asp:LinkButton>

</ItemTemplate>

</asp:FormView>

</form>

</body>

</html>
 
Y

Yuan Ren[MSFT]

Hi Paul,

Thanks for posting!

For the current issue, I create a test database from your description. When
I updating the data, there is an error indicates that the update command is
wrong. After researching, the update command likes below:
"UPDATE Car SET CarDescription =, ColorID = FROM Car INNER JOIN Color ON
Car.ColorID = Color.ColorID"

Is there anything lost near the "," syntax? Or my performing is different
with your issue? Could you please give me some details? Very thanks for
your understanding!

I'm looking forward your reply.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
 
P

P. Yanzick

Hello, thanks for your reply!

Actually, I do have queries in there for update, but I haven't finished them
off yet. I see this issue simply by pressing the Edit button on the
gridview. The DetailsView I have set as edit by default, so when I select
in the gridview, the dropdown value gets set based off of the colorID stored
in the cars table.

Let me see if I can explain a bit better. My color table currently contains
the following data:

1 White
2 Black
3 Blue
4 Green
5 Red
6 Yellow
7 Orange
8 Navy Blue
9 Grey
10 Tan
11 Silver


The numbers are the ID keys stored in the Cars table. The cars table has
the following data currently:

1 Ford F150 2
2 Ford Taurus 4
3 GMC Yukon 11


The carID is first, the description, then the colorID code. So, record 1 is
Black, 2 is Green, and 3 is Silver.

The gridview shows the data fine when I pull the page up, however if I press
the Select button to show the details in the detailsview, or if I use the
Edit button in the Gridview, the dropdown will default to the incorrect
color. As an example, record 1 in the car table will default to the color
Blue, 2 would default to Red, and interestingly record 3 defaults to White.
I hope this makes it a bit clearer. The intended result is that the
dropdown defaults to the color set in the cars table, I.E. record 1 should
be Black, 2 should be Green, and 3 should be Silver, but this isn't
appearing to be the case. I'm sure what I'm doing wrong is probably
something simple, but I can't see it and since I haven't really written a
line of code with this test project (just using VS 2005 IDE so far), I'm not
sure where the problem could be.

Thanks
Paul
 
P

P. Yanzick

Anyone have any other suggestions for me?

Thanks
Paul
P. Yanzick said:
Hello, thanks for your reply!

Actually, I do have queries in there for update, but I haven't finished
them off yet. I see this issue simply by pressing the Edit button on the
gridview. The DetailsView I have set as edit by default, so when I select
in the gridview, the dropdown value gets set based off of the colorID
stored in the cars table.

Let me see if I can explain a bit better. My color table currently
contains the following data:

1 White
2 Black
3 Blue
4 Green
5 Red
6 Yellow
7 Orange
8 Navy Blue
9 Grey
10 Tan
11 Silver


The numbers are the ID keys stored in the Cars table. The cars table has
the following data currently:

1 Ford F150 2
2 Ford Taurus 4
3 GMC Yukon 11


The carID is first, the description, then the colorID code. So, record 1
is Black, 2 is Green, and 3 is Silver.

The gridview shows the data fine when I pull the page up, however if I
press the Select button to show the details in the detailsview, or if I
use the Edit button in the Gridview, the dropdown will default to the
incorrect color. As an example, record 1 in the car table will default to
the color Blue, 2 would default to Red, and interestingly record 3
defaults to White. I hope this makes it a bit clearer. The intended
result is that the dropdown defaults to the color set in the cars table,
I.E. record 1 should be Black, 2 should be Green, and 3 should be Silver,
but this isn't appearing to be the case. I'm sure what I'm doing wrong is
probably something simple, but I can't see it and since I haven't really
written a line of code with this test project (just using VS 2005 IDE so
far), I'm not sure where the problem could be.

Thanks
Paul

"Yuan Ren[MSFT]" said:
Hi Paul,

Thanks for posting!

For the current issue, I create a test database from your description.
When
I updating the data, there is an error indicates that the update command
is
wrong. After researching, the update command likes below:
"UPDATE Car SET CarDescription =, ColorID = FROM Car INNER JOIN Color ON
Car.ColorID = Color.ColorID"

Is there anything lost near the "," syntax? Or my performing is different
with your issue? Could you please give me some details? Very thanks for
your understanding!

I'm looking forward your reply.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
 
Y

Yuan Ren[MSFT]

Hi Paul,

After researching, the current issue is caused by the setting of the
Data-Bing for the DropDownList Control. We can not bind the SelectedIndex
with the ColorID because the ColorID is start from the one but the index is
start form the zero. I suggest you bind the SelectedValue with
ColorDescription likes below:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="ColorListForDropDownDS" DataTextField="ColorDescription"
DataValueField="ColorID" SelectedValue='<%# Bind("ColorID") %>'>

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
 
Joined
Apr 23, 2007
Messages
1
Reaction score
0
hi,


i have got gridview and i have added droprdown list through template field procedure .My problem is how do i getcurrent index or value of the dropdownlist in gridview .
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top