Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filter Gridview Programmatically

Status
Not open for further replies.

JimmyFo

Programmer
Feb 14, 2005
102
US
Hi folks, I have a gridview that I would like to filter based on a radio button selection - however, the filter doesn't seem to work properly. What I'd like to do is assign parameters and filter based on the values of a column called "Status" - am I doing this the right way?

Radio:
Code:
<asp:RadioButtonList AutoPostBack="true" ID="radio_filter" ValidationGroup="other" RepeatDirection="Horizontal" runat="server">
            <asp:ListItem Selected="false" Text="All Projects" Value="All"></asp:ListItem>
            <asp:ListItem Selected="true" Text="Active Projects" Value="Active"></asp:ListItem>
            <asp:ListItem Text="Withdrawn/Completed Projects" Value="Other"></asp:ListItem></asp:RadioButtonList><br />
Gridview:
Code:
<asp:GridView ID="grid" runat="server" Width="1000px"
                    ShowHeader="true"
                    AutoGenerateColumns="false"
                    DataSourceID="DSProject"
                    EnableSortingAndPagingCallbacks="true"
                    AllowSorting = "true"
                    AllowPaging = "true"
                    PageSize="25" CellPadding="4" ForeColor="#333333" GridLines="None">
                    <Columns>
                        <asp:BoundField DataField="ProjectID" Visible="false" />
                        <asp:BoundField DataField="Number" HeaderText="Project Number" SortExpression="Number" />
                        <asp:BoundField DataField="Name" HeaderText="Project Name" SortExpression="Name" />
                        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                    </Columns>
                </asp:GridView>
SQLDataSource
Code:
<asp:SqlDataSource ID="DSProject" ConnectionString="<%$ AppSettings:SQLConnection1 %>"
            SelectCommand="uspSELECT_PROJECT"
            SelectCommandType="StoredProcedure"
            CancelSelectOnNullParameter="false"
            runat="server">
                <SelectParameters>
                    <asp:QueryStringParameter Name="p_PORTFOLIOID" QueryStringField="portfolio" />
                </SelectParameters>
            </asp:SqlDataSource>
Code Behind:
Code:
    Protected Sub radio_filter_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles radio_filter.SelectedIndexChanged
        'these parameters can be added on front end so it can be all same format
        DSProject.FilterParameters.Add("Active", "Active")
        DSProject.FilterParameters.Add("Other1", "Withdrawn")
        DSProject.FilterParameters.Add("Other2", "Completed")
        If radio_filter.SelectedValue = "All" Then
            DSProject.FilterExpression = ""
        ElseIf radio_filter.SelectedValue = "Active" Then
            DSProject.FilterExpression = "Status='{0}'"
        ElseIf radio_filter.SelectedValue = "Other" Then
            DSProject.FilterExpression = "Status='{1}' OR Status = '{2}'"
        End If
        grid.DataBind()
    End Sub

Any ideas? Thanks!

James
 
What does that mean exactly?

Well, the radio button event fires, it picks up the correcy values, but it does not work. For example, I'll have four items with different statuses, one active, two completed, one withdrawn.

I select a radio button that will put me into the "Other" branch of the if-else codebehind listed above. But it will still show only one active and one completed, but none of the others, when it is not supposed to show any "active" at all - so it's either something wrong with the programmatic filter expression, or the assignment of the filter values.

Ideas?

Thanks,
James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top