Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6872995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:00:00+00:00 2026-05-27T04:00:00+00:00

So i have a dropdownlist in a gridview that i am attempting to get

  • 0

So i have a dropdownlist in a gridview that i am attempting to get selectvalues from to update my database. My problem is: is that when press the update button, it ignores what was selected for the selected value and grabs what the first value is set to when the dropdownlist was first loaded.

So my question is: is how do obtain the selected value from a dropdownlist during the update event of a gridview.

Below you will find my codebehind, and the markup for the gridview.

codebehind:

    /// <summary>
/// Handles the Click event of the update button under edit in the gridview control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param>
protected void GridViewHolder_Updating(object sender, GridViewUpdateEventArgs e) 
{
    int machineid;
    string machineid1;
    string machineTypeid;
    string machineModelid;

    //retrieve and set the data
    GridViewHolder.EditIndex = e.RowIndex;
    BindData();

    GridViewRow row = (GridViewRow)GridViewHolder.Rows[e.RowIndex];
    TextBox mID = row.FindControl("MachineIDText") as TextBox;
    DropDownList mType = row.FindControl("MachineTypeDropDown") as DropDownList;
    DropDownList mModel = row.FindControl("MachineModelDropDown") as DropDownList;

    machineid1 = mID.Text;
    machineid = Convert.ToInt32(machineid1);
    machineTypeid = mType.SelectedValue;
    machineModelid = mModel.SelectedValue;

    try
    {
        if (machineTypeid != "empty" || machineModelid != "empty")
        {
            if (machineTypeid != "empty")
            {
                inputsService.UpdateMachineTypes(machineid, machineTypeid);
            }
            if (machineModelid != "empty")
            {
                inputsService.UpdateMachineModels(machineid, machineModelid);
            }
            UpdateSucceed.Visible = true;
            logger.Debug("Updating - Database successfully updated!");
        }
        else
        {
            UpdateFail.Visible = true;
            logger.Debug("Updating - Database had no data selected to be updated.");
        }
    }
    catch (Exception ex)
    {
        logger.ErrorFormat("Updating - Failed to update the table, ex = {0}", ex);
    }
}

/// <summary>
/// Handles the Click event of the cancel button under edit in the gridview control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCancelEditEventArgs"/> instance containing the event data.</param>
protected void GridViewHolder_Canceling(object sender, GridViewCancelEditEventArgs e)
{
    //reset the edit index
    GridViewHolder.EditIndex = -1;
    //Bind data to GridViewHolder
    BindData();
}

protected void GridViewHolder_DataBound(object sender, GridViewRowEventArgs e)
{
    if (this.GridViewHolder.EditIndex != -1)
    {
        DropDownList mType = e.Row.FindControl("MachineTypeDropDown") as DropDownList;
        DropDownList mModel = e.Row.FindControl("MachineModelDropDown") as DropDownList;
    }
}

protected void GridViewHolder_Editing(object sender, GridViewEditEventArgs e)
{
    //set the edit index to a new value
    GridViewHolder.EditIndex = e.NewEditIndex;
    //Bind data to gridviewholder
    BindData();
}

#endregion

#region Private Methods

private void BindData()
{
    GridViewHolder.DataSource = Session["MachineTable"];
    GridViewHolder.DataBind();
}

gridview markup:

<asp:GridView ID="GridViewHolder" 
                      runat="server" 
                      AllowPaging="True" 
                      AutoGenerateColumns="False" 
                      BackColor="Transparent" 
                      BorderColor="#999999" 
                      BorderStyle="Ridge" 
                      BorderWidth="3px" 
                      CellPadding="4" 
                      CellSpacing="2" 
                      DataSourceID="MachineDataSet" 
                      ForeColor="Black" 
                      HeaderStyle-HorizontalAlign="Center" 
                      HorizontalAlign="Center"  
                      RowStyle-HorizontalAlign="Center" 
                      Width="796px"
                      OnRowUpdating="GridViewHolder_Updating"
                      OnRowCancelingEdit="GridViewHolder_Canceling"
                      OnRowEditing="GridViewHolder_Editing"
                      OnRowDataBound="GridViewHolder_DataBound"                          
                      EnableViewState="False">
            <RowStyle BackColor="Transparent" 
                      HorizontalAlign="Center" />
            <Columns>
                <asp:TemplateField HeaderText="ID" 
                                   SortExpression="ID" 
                                   Visible="False">
                    <ItemTemplate>
                        <asp:Label ID="MachineIDLabel" 
                                   runat="server" 
                                   Text='<%# Bind("ID") %>'
                                   Visible="false"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="MachineIDText" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="SiteName" 
                                HeaderText="Site Name" 
                                SortExpression="SiteName"
                                ReadOnly="true" />
                <asp:BoundField DataField="Name" 
                                HeaderText="Machine Name" 
                                ReadOnly="true" 
                                SortExpression="Name" />
                <asp:TemplateField HeaderText="Machine Type" 
                                   SortExpression="MachineType">
                    <ItemTemplate>
                        <asp:Label ID="MachineTypeLabel" 
                                   runat="server" 
                                   Text='<%# Bind("MachineType") %>'>
                        </asp:Label>                            
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="MachineTypeDropDown" 
                                          runat="server" 
                                          AppendDataBoundItems="True"                                                
                                          Height="21px" 
                                          Width="217px" 
                                          DataSourceID="GetMachineType" 
                                          DataTextField="Name" 
                                          DataValueField="ID">
                            <asp:ListItem Enabled="true" 
                                          Text="Select a Machine Type." 
                                          Value="empty">
                            </asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Machine Model" SortExpression="MachineModel">
                    <ItemTemplate>
                        <asp:Label ID="MachineModelLabel" 
                                   runat="server" 
                                   Text='<%# Bind("MachineModel") %>'>
                        </asp:Label>                            
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="MachineModelDropDown" 
                                          runat="server" 
                                          AppendDataBoundItems="True"                                                
                                          Height="21px" Width="217px" 
                                          DataSourceID="GetMachineModel" 
                                          DataTextField="Name" 
                                          DataValueField="ID">
                            <asp:ListItem Enabled="true" 
                                          Text="Select a Machine Model." 
                                          Value="empty">
                            </asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ButtonType="Button" 
                                  ShowEditButton="True"
                                  CausesValidation="false" >
                    <ItemStyle HorizontalAlign="Center" 
                               Wrap="True" />
                </asp:CommandField>
            </Columns>
            <FooterStyle BackColor="Transparent" />
            <PagerStyle BackColor="Transparent" 
                        ForeColor="Black" 
                        HorizontalAlign="Left" />
            <SelectedRowStyle BackColor="Transparent" 
                              Font-Bold="True" 
                              ForeColor="White" />
            <HeaderStyle BackColor="Black" 
                         Font-Bold="True" 
                         ForeColor="White" 
                         HorizontalAlign="Center" />
     </asp:GridView>

any help or suggestions are greatly appreciated,

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T04:00:01+00:00Added an answer on May 27, 2026 at 4:00 am

    The RowUpdating event has event-arguments of type GridViewUpdateEventArgs. This has dictionaries for key-, old- and new-values. There is no need to get them by using FindControl on the GridViewRow.

    But the main reason why you are getting the old values is the fact that you are DataBinding it again in this event. Then you’re overwriting all changes with the values from your DataSource.

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.aspx

    Update: it seems that the DropDownList‘s SelectedValue is not part of the NewValues dictionary. Therefore you could add it in the RowUpdating event.

    protected void GridViewHolder_Updating(object sender, GridViewUpdateEventArgs e) 
    {
        GridViewRow row = (GridViewRow)GridViewHolder.Rows[e.RowIndex];
        DropDownList mType = row.FindControl("MachineTypeDropDown") as DropDownList;
        e.NewValues.Add("MachineType", mType.SelectedValue):
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DropDownList with values that I get from a SQL database. Depending
I have a dropdownlist inside a gridview, that is loaded from a datasource when
I have a gridview that imports data from a remote database, this is working
I have a DropDownList that populates a GridView according to the selected value. I
I have a gridview, that shows record according to the selected value in the
I have a gridview in an updatepanel where also my dropdownlist is. From the
I have a gridview button that I programmatically created and I want to load
I have a dropdownlist that controls the contents of 3 gridview controls. These are
I have got 2 DropDownList s on my Form and 1 GridView . I
I have tried different examples to filter a gridview by dropdownlist, but is it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.