?
Solved

DropDownList in GridView not Updating Table (ASP.NET  C#)

Posted on 2009-05-01
2
Medium Priority
?
1,478 Views
Last Modified: 2012-06-21
I have an ASP.NET page with a GridView. The GridViews 'Project List' contents are generated from query string from a 'Clients' page. When editing I want the 'Status' field to be a DropDownList with 3 Selections available (Active,Pending & Complete). The page looks ok but on updating the 'Status' field keeps it's previous value? I have created a test GridView below the first grid & populated the grid directly from a SqlDataSource, using similar code I can update the Status field no problem. Why wont the first GridView update correctly? The only difference from the 2nd GridView is the QueryString.

Any Help Appreciated.
Thanks
Paul.
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="ClientProjects.aspx.cs" Inherits="Admin_ClientProjects" Title="Timesheets: Client Projects" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <br />
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Admin/ManageClientProjects.aspx">Back to Clients</asp:HyperLink><br />
    <br />
    <asp:Label ID="laProjectMessage" runat="server" ForeColor="Red" Text="laProjectMessage"></asp:Label><br />
    <br />
    <asp:GridView ID="GridViewProjects" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        CellPadding="4" DataKeyNames="ProjectDescription" DataSourceID="SqlDataSourceClientProjects"
        ForeColor="#333333" GridLines="None" PageSize="20" OnRowDataBound="GridViewProjects_RowDataBound">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:HyperLinkField DataNavigateUrlFields="ProjectNumber" DataNavigateUrlFormatString="ClientSubProjects.aspx?ProjectNumber={0}"
                Text="Sub Projects" />
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="ProjectNumber" HeaderText="ProjectNumber" ReadOnly="True"
                SortExpression="ProjectNumber" />
            <asp:BoundField DataField="ProjectDescription" HeaderText="ProjectDescription" SortExpression="ProjectDescription" />
            <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" />
            <asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
            <asp:TemplateField HeaderText="Status" SortExpression="Status">
                <EditItemTemplate>
                    <asp:DropDownList ID="ProjectsDropDownListStatusE" runat="server" DataSourceID="SqlDataSourceClientProjects"
                        DataTextField="Status" DataValueField="Status"
                        SelectedValue='<%# Bind("Status") %>' Width="138px">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <EmptyDataTemplate>
            Client has no Projects
        </EmptyDataTemplate>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSourceClientProjects" runat="server" ConnectionString="<%$ ConnectionStrings:TimeSheetsConnectionString %>"
        DeleteCommand="DELETE FROM [Projects] WHERE [ProjectDescription] = @ProjectDescription" InsertCommand="INSERT INTO [Projects] ([ProjectNumber], [ProjectDescription], [Client], [Hours], [Status]) VALUES (@ProjectNumber, @ProjectDescription, @Client, @Hours, @Status)"
        SelectCommand="SELECT [ProjectNumber], [ProjectDescription], [Client], [Hours], [Status] FROM [Projects] WHERE ([Client] = @Client)"
        UpdateCommand="UPDATE [Projects] SET [ProjectDescription] = @ProjectDescription, [Client] = @Client, [Hours] = @Hours, [Status] = @Status WHERE [ProjectNumber] = @ProjectNumber" OnDeleted="SqlDataSourceClientProjects_Deleted">
        <DeleteParameters>
            <asp:Parameter Name="ProjectDescription" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="ProjectDescription" Type="String" />
            <asp:Parameter Name="Client" Type="String" />
            <asp:Parameter Name="Hours" Type="Double" />
            <asp:Parameter Name="Status" Type="String" />
            <asp:Parameter Name="ProjectNumber" Type="String" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="ProjectNumber" Type="String" />
            <asp:Parameter Name="ProjectDescription" Type="String" />
            <asp:Parameter Name="Client" Type="String" />
            <asp:Parameter Name="Hours" Type="Double" />
            <asp:Parameter Name="Status" Type="String" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="Client" QueryStringField="Client" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    <br />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        DataKeyNames="ProjectNumber" DataSourceID="SqlDataSourceClientProjects" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:CommandField ShowEditButton="True" />
            <asp:BoundField DataField="ProjectNumber" HeaderText="ProjectNumber" ReadOnly="True"
                SortExpression="ProjectNumber" />
            <asp:BoundField DataField="ProjectDescription" HeaderText="ProjectDescription" SortExpression="ProjectDescription" />
            <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" />
            <asp:BoundField DataField="Hours" HeaderText="Hours" SortExpression="Hours" />
            <asp:TemplateField HeaderText="Status" SortExpression="Status">
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSourceClientProjects"
                        DataTextField="Status" DataValueField="Status" SelectedValue='<%# Bind("Status") %>'
                        Width="169px">
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</asp:Content>
 
In Code Behind File...
 
protected void GridViewProjects_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
 
            DropDownList ProjectsStatusDDL = (DropDownList)e.Row.FindControl("ProjectsDropDownListStatusE");
 
            string ProjectsStatusValue = ProjectsStatusDDL.SelectedValue.Trim();
 
            ProjectsStatusDDL.Items.Clear();
 
            ProjectsStatusDDL.Items.Add("Active");
            ProjectsStatusDDL.Items.Add("Pending");
            ProjectsStatusDDL.Items.Add("Complete");
 
            int ProjectsStatusIndex = ProjectsStatusDDL.Items.IndexOf(ProjectsStatusDDL.Items.FindByText(ProjectsStatusValue));
            ProjectsStatusDDL.SelectedIndex = ProjectsStatusIndex;
 
        }
 
    }
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 
        //Test GridView
        if ((e.Row.RowState & DataControlRowState.Edit) > 0)
        {
 
            DropDownList ProjectsStatusDDL = (DropDownList)e.Row.FindControl("DropDownList1");
 
            string ProjectsStatusValue = ProjectsStatusDDL.SelectedValue.Trim();
 
            ProjectsStatusDDL.Items.Clear();
 
            ProjectsStatusDDL.Items.Add("Active");
            ProjectsStatusDDL.Items.Add("Pending");
            ProjectsStatusDDL.Items.Add("Complete");
 
            int ProjectsStatusIndex = ProjectsStatusDDL.Items.IndexOf(ProjectsStatusDDL.Items.FindByText(ProjectsStatusValue));
            ProjectsStatusDDL.SelectedIndex = ProjectsStatusIndex;
 
        }
 
    }

Open in new window

0
Comment
Question by:pr_wainwright
2 Comments
 
LVL 15

Accepted Solution

by:
NazoUK earned 500 total points
ID: 24278098
Your datakeynames are different in the 2 gridviews. The DataKeyName should correspond to the primary key of the record you are tryign to update.
0
 
LVL 1

Author Closing Comment

by:pr_wainwright
ID: 31576812
NazoUK,
              Worked great. Sorry for the delay, been away for the Bank Holiday.

Regards
Paul.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month17 days, 6 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question