[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.Net Gridview filtered by dropdown list

Posted on 2010-01-08
13
Medium Priority
?
474 Views
Last Modified: 2012-06-27
I have an ASP.Net page that displays data from a table in an Access database in a gridview. I've also put a drop down list on the page. The drop down list is also from a different table in the same database. I want the user to be able to select a year (i.e., 2004) and have the gridview filtered to just show data from that year. How can this be donw with ASP.Net? I'm using C# as the code behind. I'm hoping to find examples somewhere. Thanks.
0
Comment
Question by:dodgerfan
  • 7
  • 5
13 Comments
 
LVL 61

Expert Comment

by:HainKurt
ID: 26212525
how do you get the data
and how do you set & bind data to gridview...

can you please post code for this...
0
 
LVL 11

Expert Comment

by:princeatapi
ID: 26212534
Add a SelectedIndex Changed event handler  to the Drop Down List
write a handler similar to this
protected void DDList_SelectedIndexChanged(object sender, EventArgs e)
    {
        MyGrid.DataSource = Getlist(DDList.SelectedIndex);
        MyGrid.DataBind();
    }

in this case GetList() is a User defined function which would return a DataTable / Suitable DataSource to the Gridview from the Database based on the selection made in DropDownList
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26212584
say you used a datatable as source of grid, then do this

dim dt as datatable = gridview.datasource
dt.defaultview.rowfilter = "year = " & ddYear.selectedValue

'may not be needed
gridview.datasource = dt
gridview.DataBind

if you are using sqldatasource you can try this

MySqlDataSource.FilterExpression = "year = " & ddYear.selectedValue
gridview.DataBind
0
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.

 
LVL 61

Expert Comment

by:HainKurt
ID: 26212615
or

protected void DDList_SelectedIndexChanged(object sender, EventArgs e) {
  DataSource ds = GetMyData();
  ds.tables[0].RowFilter = "year = " & ddYear.selectedValue;
  MyGrid.DataSource = ds.tables[0].DefaultView;
  MyGrid.DataBind();
}

or

protected void DDList_SelectedIndexChanged(object sender, EventArgs e) {
  DataTable dt = GetMyData();
  dt.RowFilter = "year = " & ddYear.selectedValue;
  MyGrid.DataSource = dt.DefaultView;
  MyGrid.DataBind();
}
0
 

Author Comment

by:dodgerfan
ID: 26212624
I don't have access to the code right now. I will post what I have this evening. The gridview is built using a table in MS Access. So it's an Access Datasource. Thanks.
0
 

Author Comment

by:dodgerfan
ID: 26273725
I've attached the code for the page as currently constructed. There is no code behind yet.
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="AuctionDrafts.aspx.cs" Inherits="History_Winners" Title="Yearly Winners" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div style="margin-left:25px; margin-top:15px">
<br /><br /><br />
    <table width="35%" align="center" cellpadding="3" border="1">
        <tr>
            <td>
                <asp:Label ID="lblYear" runat="server" Text="Select Year:" Font-Bold="True" 
                    Font-Names="Verdana" Font-Size="Small"></asp:Label>
                &nbsp;&nbsp;&nbsp;
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
                    DataSourceID="Baseball" DataTextField="Year" DataValueField="Year" 
                    Width="120px" Font-Names="Verdana" Font-Size="Small">
                </asp:DropDownList>
            </td>
        </tr>
        <tr><td>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
                CellPadding="3" DataSourceID="DraftData" GridLines="Vertical" 
                Width="600px">
                <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
                <Columns>
                    <asp:BoundField DataField="OrderNo" HeaderText="Order No." 
                        SortExpression="OrderNo" >
                        <ItemStyle Font-Size="12px" HorizontalAlign="Center" Font-Names="Verdana" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="12px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Year" HeaderText="Year" SortExpression="Year" >
                        <ItemStyle Font-Size="12px" HorizontalAlign="Center" Font-Names="Verdana" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="12px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Player" HeaderText="Player" SortExpression="Player" >
                        <ItemStyle Font-Size="12px" HorizontalAlign="Center" Font-Names="Verdana" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="12px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" >
                        <ItemStyle Font-Size="12px" HorizontalAlign="Center" Font-Names="Verdana" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="12px" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Salary" HeaderText="Salary" SortExpression="Salary" >
                        <ItemStyle Font-Size="12px" HorizontalAlign="Center" Font-Names="Verdana" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="12px" />
                    </asp:BoundField>
                </Columns>
                <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="#DCDCDC" />
            </asp:GridView>
            <asp:AccessDataSource ID="DraftData" runat="server" 
                DataFile="~/App_Data/BaseballXP_Working.mdb" 
                SelectCommand="SELECT [OrderNo], [Year], [Player], [Owner], [Salary] FROM [DraftDayHistory]">
            </asp:AccessDataSource>
        </td></tr>
        <asp:AccessDataSource ID="Baseball" runat="server" 
            DataFile="~/App_Data/BaseballXP_Working.mdb" 
            
            SelectCommand="SELECT DISTINCT [Year] FROM [DraftDayHistory] ORDER BY [Year]">
        </asp:AccessDataSource>
    </table>  
</div> </asp:Content>

Open in new window

0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26275985
use this...
protected void DDList_SelectedIndexChanged(object sender, EventArgs e) {
  DraftData.SelectCommand = "SELECT [OrderNo], [Year], [Player], [Owner], [Salary] FROM [DraftDayHistory] where [Year] = " & ddYear.selectedValue;
  GridView1.DataBind();
}

Open in new window

0
 

Author Comment

by:dodgerfan
ID: 26276136
Still  not working yet. It compiles fine, but it does not filter anything. This is what I have now from your suggestion:
  protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        DraftData.SelectCommand = "SELECT [OrderNo], [Year], [Player], [Owner], [Salary] FROM [DraftDayHistory] where [Year] = " + ddlYear.SelectedValue;
        GridView1.DataBind();
    }
It did not like the & used at the end of the Select command. So I changed it to a +. I also changed mr drop down list to this:
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="True">
     <asp:ListItem>2003</asp:ListItem>
     <asp:ListItem>2004</asp:ListItem>
      <asp:ListItem>2005</asp:ListItem>
      <asp:ListItem>2006</asp:ListItem>
      <asp:ListItem>2007</asp:ListItem>
      <asp:ListItem>2008</asp:ListItem>
      <asp:ListItem>2009</asp:ListItem>
 </asp:DropDownList>

Thanks again for the help.
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26276176
it did not filter anything or it did not show anything?
can you please put a breakpoint on line 2 @ 26275985, and check the value of DraftData.SelectCommand before and after that line...

also

ddlYear.SelectedValue
-->
ddlYear.SelectedText or ddlYear.SelectedItem.Text

since you do not set the value... or the value may be set to text by default, in this case ddlYear.SelectedValue should be fine (I am not sure now)
0
 
LVL 61

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 26276188
or we can try this

<asp:AccessDataSource ID="DraftData" runat="server"  
                DataFile="~/App_Data/BaseballXP_Working.mdb"  
                SelectCommand="SELECT [OrderNo], [Year], [Player], [Owner], [Salary] FROM [DraftDayHistory]"> 
            </asp:AccessDataSource> 

-->

<asp:AccessDataSource ID="DraftData" runat="server"  
                DataFile="~/App_Data/BaseballXP_Working.mdb"  
                SelectCommand="SELECT [OrderNo], [Year], [Player], [Owner], [Salary] FROM [DraftDayHistory] where ([Year] = @SelectedYear) or (@SelectedYear=0)"> 
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlYear" Name="SelectedYear" PropertyName="SelectedValue" defaultvalue="0"/>
        </SelectParameters>
            </asp:AccessDataSource> 

Open in new window

0
 
LVL 61

Expert Comment

by:HainKurt
ID: 26276190
above solution needs no extra code... if it works as you want ;)
0
 

Author Comment

by:dodgerfan
ID: 26276207
It does not filter anything. I have the gridview showing all data to start. I switched the code per your last suggestion, too. It's now ddlYear.SelectedItem.Text. Still no change. For the life of me I can;t get it debugging properly. Still trying.
0
 

Author Closing Comment

by:dodgerfan
ID: 31674702
Absolutely perfect. Thanks again for all of your help. This is great.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

831 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