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

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

LVL 1
pr_wainwrightAsked:
Who is Participating?
 
NazoUKCommented:
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
 
pr_wainwrightAuthor Commented:
NazoUK,
              Worked great. Sorry for the delay, been away for the Bank Holiday.

Regards
Paul.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.