'Dropedownlist Select All option need to show all records in gridView using C# in asp.net

I have added "Select All" option on to the Dropdownlist which is bound to a column "Clinic". When any of the Clinics is selected it shows data for the selected Clinic, which is working well as required. However, I want to show all Clinic records when the "Selected All" option is selected. How do I do that please?



Solution 1:[1]

Well, you should at least post "some" code here.

but, say we have a grid, list of hotels.

And we want a filter combo box by say city. So, we might have say this markup:

        <asp:Label ID="Label1" runat="server" Text="Hotels" Font-Size="Large"></asp:Label>
        <asp:Label ID="Label2" runat="server" Text="Filter by City" Font-Size="Large"
            Style="margin-left:40px;margin-right:10px"></asp:Label>

        <asp:DropDownList ID="cboCity" runat="server" Width="190px" Height="28"
            DataValueField="City"
            DataTextField="City" OnSelectedIndexChanged="cboCity_SelectedIndexChanged"
            AutoPostBack="true">
        </asp:DropDownList>

        <br />
        <br />
        <asp:GridView ID="GHotels" runat="server" CssClass="table"
            width="50%" AutoGenerateColumns="False" DataKeyNames="ID" >
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"     />
                <asp:BoundField DataField="LastName" HeaderText="LastName"       />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName"     />
                <asp:BoundField DataField="City" HeaderText="City"               />
                <asp:BoundField DataField="Description" HeaderText="Description" />
            </Columns>
        </asp:GridView>

Our code to load can be this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCityCbo();
            LoadGrid("");
         }
    }

    void LoadCityCbo()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL = "SELECT City from City ORDER BY City";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                conn.Open();
                cboCity.DataSource = cmdSQL.ExecuteReader();
                cboCity.DataBind();
                cboCity.Items.Insert(0, "Show All");
            }
        }
    }

    void LoadGrid(string sCity)
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL = "SELECT TOP 10 * FROM tblHotels";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                if (sCity != "")
                {
                    // city filter passed
                    cmdSQL.CommandText += " WHERE City = @City";
                    cmdSQL.Parameters.Add("@City", SqlDbType.NVarChar).Value = sCity;
                }
                cmdSQL.CommandText += " ORDER BY HotelName";
                DataTable rstData = new DataTable();
                conn.Open();
                rstData.Load(cmdSQL.ExecuteReader());
                GHotels.DataSource = rstData;
                GHotels.DataBind();
            }
        }
    }

    protected void cboCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cboCity.SelectedIndex == 0)
        {
            // load grid - all rows without filter
            LoadGrid("");
        }
        else
            LoadGrid(cboCity.Text);
    }
}

And we now see this:

enter image description here

So, if you select the first "select all" in the cbo, then we simple skip the condition. If you select a city, then we pass that city to the load grid routine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1