Advertisement

03.13.2008 at 11:48AM PDT, ID: 23239688
[x]
Attachment Details

How to put a dropdownlist in a gridview and update the database on user clicking update

Asked by gbzhhu in Programming for ASP.NET, Microsoft Visual Basic.Net, Microsoft Visual C#.Net

Hi,

I am on VS 2005 with an asp.net.  I have a gridview which displays user table - simple things like firstname, lastname AND user type which is Secretary, Manager etc.

I have 2 tables user and userType(only with typeId and description fields).  User table has a foreignkey field userType which references the typeId of userType table.  Now how do I add a dropdownlist that on itemtemplate shows the description of the userType and on edittemplate shows a dropdiown with all the userTypes.  When user clicks on Update, I would like to update the user table so that userType field points to the selected userType in userType table.  Hope I am making sense.  Code I have so far is attached.  GetUserTypes returns a dataset reading userType table.  This line below causes error
<asp:Label Text="<%# Bind("description") %>" runat="server"/>

I am not sure I am doing the whole thing right.

Help appreciated.

H
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
<asp:GridView ID="gv_users" runat="server" DataSourceID="UserData" DataKeyNames="userId" OnDataBound="gv_users_DataBound" OnRowDataBound="gv_users_RowDataBound" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" PageSize="5" Width="600px" 
             AutoGenerateDeleteButton="True" AutoGenerateEditButton="True">
            <Columns>
                <asp:BoundField DataField="userID" HeaderText="userId" ReadOnly="True" SortExpression="userId" Visible="False" />
 
                <asp:TemplateField HeaderText="User Type">
                    <EditItemTemplate>
                        <asp:Label Text="<%# Bind("description") %>" runat="server"/>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:DropDownList DataSource='<%# GetUserTypes() %>' DataTextField="description" DataValueField="type" ID="ddlUserType" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                    
                </asp:TemplateField>
                          
                <asp:BoundField DataField="firstName" HeaderText="First Name" SortExpression="firstName" />
                <asp:BoundField DataField="lastName" HeaderText="Last Name" SortExpression="lastName" />
                <asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" HeaderStyle-Width="200" />
            </Columns>
            
            <pagerstyle backcolor="#999999" forecolor="Black" horizontalalign="Center" />   
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="Gray" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
            <EditRowStyle BackColor="#7C6F57" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <asp:SqlDataSource ID="UserData" runat="server" ConnectionString="<%$ ConnectionStrings:DBTConnectionString %>" 
            SelectCommand="SELECT * FROM PFC_users u join PFC_xref_userType ut on u.userType = ut.type" 
            InsertCommand="INSERT INTO PFC_users(firstName, lastName, country) VALUES(@firstName, @lastName, @country)"
            DeleteCommand="DELETE FROM PFC_users WHERE userID = @userID " 
            UpdateCommand="UPDATE PFC_users SET firstName = @firstName, lastName = @lastName, country = @country WHERE userID = @userID">             
        </asp:SqlDataSource>
[+][-]03.13.2008 at 12:04PM PDT, ID: 21119603

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Programming for ASP.NET, Microsoft Visual Basic.Net, Microsoft Visual C#.Net
Sign Up Now!
Solution Provided By: Velio
Participating Experts: 2
Solution Grade: A
 
 
[+][-]03.14.2008 at 12:55AM PDT, ID: 21123609

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.14.2008 at 03:33AM PDT, ID: 21124185

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.14.2008 at 03:53AM PDT, ID: 21124278

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.14.2008 at 04:15AM PDT, ID: 21124382

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.14.2008 at 04:18AM PDT, ID: 21124391

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.14.2008 at 04:22AM PDT, ID: 21124406

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.14.2008 at 04:31AM PDT, ID: 21124441

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628