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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grid View with Custom Sorting and Paging

Status
Not open for further replies.

raphael232

Programmer
Joined
Jun 9, 2006
Messages
51
Location
GB
Hi, i am trying to do custom paging and sorting on my site for the grid view control. So far i have the following:

Code:
    <asp:TextBox ID="txtSearch" Text="" runat="server"></asp:TextBox>
    <asp:Button ID="btnSearch" runat="server" Text="Search" /><br />
    <br />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        CssClass="Table" DataKeyNames="ID" DataSourceID="ObjectDataSource1" AllowSorting="True">
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="fldPublishDate" HeaderText="Publish Date" SortExpression="fldPublishDate" />
            <asp:BoundField DataField="fldTitle" HeaderText="Title" SortExpression="fldTitle" />
        </Columns>
    </asp:GridView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"
        OldValuesParameterFormatString="original_{0}" EnablePaging="True" SortParameterName="SortBy" SelectMethod="GetNewsSubset" SelectCountMethod="GetNewsCountBySearch" TypeName="KITTableAdapters.tbdNewsTableAdapter"
        UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_ID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="fldPublishDate" Type="DateTime" />
            <asp:Parameter Name="fldTitle" Type="String" />
        </UpdateParameters>
        <SelectParameters>
            <asp:ControlParameter ControlID="txtSearch" Name="SearchBy" PropertyName="Text" Type="String" />
        </SelectParameters>
    </asp:ObjectDataSource>

I have setup 2 querys in my tbdNewsTableAdapter table adapter:

1. GetNewsCountBySearch(@SearchBy):

SELECT COUNT(*) FROM qryNews WHERE fldTitle LIKE '%' + @SearchBy + '%'

2. GetNewsSubset(@SearchBy, @SortBy, @StartRowIndex, @MaximumRows) - which calls the stored procedure:

Code:
ALTER PROCEDURE [dbo].[spGetNewsSubset]
	@SearchBy VARCHAR(255),
	@SortBy VARCHAR(255),
	@StartRowIndex INT,
	@MaximumRows INT
AS
BEGIN
	WITH NewsEntries AS (
		SELECT ROW_NUMBER() OVER (ORDER BY @SortBy) AS RowNum, *
		FROM qryNews
		WHERE fldTitle LIKE '%' + @SearchBy + '%'
	)

	SELECT *
	FROM NewsEntries
	WHERE RowNum BETWEEN @StartRowIndex + 1 AND @StartRowIndex + @MaximumRows
END

I'm getting two problems:

1. I seem to have the custom paging working fine but the sorting does not seem to do anything.

2. The other problem i'm getting is that if the txtSearchBy is empty it is sending to GetNewsCountBySearch as null and subsequently returning 0. However i wish for it to return all the results if the txtSearchBy is empty.

Appreciate if someone could take the time to have a look. Thanks
 
>>1. I seem to have the custom paging working fine but the sorting does not seem to do anything.

how are you confirming this? does the sort event itself not fire?
one more thing, are you adding dynamic columns to the gridview (using code)...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top