locked
Looping through rows filtered by FilterExpression RRS feed

  • Question

  • User-1144820319 posted

    I have a GridView and a SQLDataSource. I use the FilterExpression to query from the full result and filter out. Now I would like calculate some values on the filtered rows. How can I retrieve the table/rows from the filtered rows only?

     I know that I can get the Table from the RowDataBound event but that is all rows - I am only interested in the ones that are filtered out. Any ideas?

    Thanks.

    Tuesday, April 7, 2009 6:02 PM

Answers

  • User2011918074 posted

    Hi,

    The SqlDataSource.FilterExpression property is applied any time the SqlDataSource.Select method is executed to retrieve data.

    Please refer to the sample below:

    WebForm1.aspx: 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                SortExpression="ProductName" />
            <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" 
                SortExpression="UnitsInStock" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:northwindConnectionString %>" 
        FilterExpression="ProductName LIKE '*art*'" 
        SelectCommand="SELECT [ProductID], [ProductName], [UnitsInStock] FROM [Products]">
    </asp:SqlDataSource>

    WebForm1.aspx.cs: 

    protected void Page_Load(object sender, EventArgs e)
    {
        DataView dv = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
        Int16 sumstock = 0;
    
        foreach (DataRowView dr in dv)
        {
            sumstock += (Int16)dr["UnitsInStock"];
        }
    
        Response.Write(sumstock.ToString());
    }

    Related documents:

    SqlDataSource.FilterExpression Property
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.filterexpression.aspx

    SqlDataSource.Select Method
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.select.aspx  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2009 3:21 AM

All replies

  • User2011918074 posted

    Hi,

    The SqlDataSource.FilterExpression property is applied any time the SqlDataSource.Select method is executed to retrieve data.

    Please refer to the sample below:

    WebForm1.aspx: 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ProductID" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="ProductID" HeaderText="ProductID" 
                InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
            <asp:BoundField DataField="ProductName" HeaderText="ProductName" 
                SortExpression="ProductName" />
            <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" 
                SortExpression="UnitsInStock" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:northwindConnectionString %>" 
        FilterExpression="ProductName LIKE '*art*'" 
        SelectCommand="SELECT [ProductID], [ProductName], [UnitsInStock] FROM [Products]">
    </asp:SqlDataSource>

    WebForm1.aspx.cs: 

    protected void Page_Load(object sender, EventArgs e)
    {
        DataView dv = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
        Int16 sumstock = 0;
    
        foreach (DataRowView dr in dv)
        {
            sumstock += (Int16)dr["UnitsInStock"];
        }
    
        Response.Write(sumstock.ToString());
    }

    Related documents:

    SqlDataSource.FilterExpression Property
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.filterexpression.aspx

    SqlDataSource.Select Method
    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.select.aspx  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 10, 2009 3:21 AM
  • User-1144820319 posted

    Perfect! Thanks!

    Friday, April 10, 2009 3:34 AM