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 6551607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:21:29+00:00 2026-05-25T12:21:29+00:00

I have a GridView with a DropDownList in each row. (The items in the

  • 0

I have a GridView with a DropDownList in each row. (The items in the DropDownList are the same for each.) I have a DropDownList “ddlView” outside of the GridView that is used for filtering the available options in the other DropDownLists. The default selection for ddlView is no filter.

When a user selects a new value for ddlView any selected values in the other DropDownLists disappear if they are not one of the values present after the filter is applied. What I would like to happen in that case is the previously selected value still be present and selected.

What is the best way to accomplish this?

The previously selected values are available during postback but appear to be cleared once DataBind() is called on the GridView, so I am unable to determine their previous value in the method where they are populated (the RowDataBound event).

My best idea so far is to manually store that information into an object or collection during postback and reference it later during the databinding events.

Is there a better way?

  • 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-25T12:21:30+00:00Added an answer on May 25, 2026 at 12:21 pm

    I don’t think there is a better way to accomplish this as when the GridView is bound all of the controls are recreated thus removing the selections.

    The following works: (I store the selections on postback to retrieve again in the RowDataBound event)

    Markup

    <asp:Button ID="button1" runat="server" Text="Post Back" OnClick="button1_Click" />
    <br />
    <asp:GridView ID="gridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:DropDownList ID="gridViewDropDownList" runat="server">
                        <asp:ListItem Value="1">Item 1</asp:ListItem>
                        <asp:ListItem Value="2">Item 2</asp:ListItem>
                        <asp:ListItem Value="3">Item 3</asp:ListItem>
                    </asp:DropDownList>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    

    Code

    public class GridViewDropDownSelections
    {
        public int RowIndex { get; set; }
        public int SelectedIndex { get; set; }
    }
    
    ...
    
    private List<GridViewDropDownSelections> selectedDropDownListItems = new List<GridViewDropDownSelections>();
    
    protected override void OnLoad(EventArgs e)
    {
        var selections = gridView1.Rows.Cast<GridViewRow>().Where(r => r.RowType == DataControlRowType.DataRow)
            .Select(r => new GridViewDropDownSelections() { RowIndex = r.RowIndex, SelectedIndex = ((DropDownList)r.FindControl("gridViewDropDownList")).SelectedIndex }).ToList();
    
        selectedDropDownListItems.AddRange(selections);
    
        gridView1.RowDataBound += new GridViewRowEventHandler(gridView1_RowDataBound);
    
        if (!IsPostBack)
        {
            BindDataGrid();
        }
    
        base.OnLoad(e);
    }
    
    protected void button1_Click(object sender, EventArgs e)
    {
        BindDataGrid();
    }
    
    private void BindDataGrid()
    {
        //Dummy data
        string[] data = new string[] { "Item 1", "Item 2", "Item 3" };
        gridView1.DataSource = data;
        gridView1.DataBind();
    }
    
    void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var selection = selectedDropDownListItems.FirstOrDefault(i => i.RowIndex == e.Row.RowIndex);
            if (selection != null)
            {
                try
                {
                    DropDownList gridViewDropDownList = (DropDownList)e.Row.FindControl("gridViewDropDownList");
                    gridViewDropDownList.SelectedIndex = selection.SelectedIndex;
                }
                catch (Exception)
                {
                }
            }
        }
    }
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dropdownlist inside a gridview, that is loaded from a datasource when
I have a GridView, each row has Edit button. After it's clicked, one of
I have a table that has Edit and Delete links in each row. I
I have a gridview in which every row contains a dropdownlist. I want to
I have created a ASP.NET WebForms Gridview and I need to access each row
I'm currently building a Gridview that has expandable rows. Each row contains a dynamically
I have a Gridview in which i have two templatefields of dropdownlist. I bound
I have tried different examples to filter a gridview by dropdownlist, but is it
I have an ASP.NET GridView with a DropDownList (in an Item Template) and a
I have got 2 DropDownList s on my Form and 1 GridView . I

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.