GridView page changing doesn't work (Non SQL Source)

S

sck10

Hello,

I have a GridView that I built to show the files in a directory. The
problem that I am having is that when I click on the second page (2), the
GridView disappears. Also, I can't sort by any of the columns.

Below is the code that I am using. Any help would be appreciated.

Thanks, sck10



<asp:GridView ID="gvSearchList" runat="server"
AllowPaging="True"
AllowSorting="True"
AutoGenerateSelectButton="False"
AutoGenerateColumns="False"
PageSize="15"
style="width:70%"
OnPageIndexChanging="gvSearchList_PageIndexChanging"
OnSorting="gvSearchList_Sorting"
OnSelectedIndexChanged="gvSearchList_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Name"
HeaderText="File Name"
SortExpression="Name"
HeaderStyle-VerticalAlign="Bottom"
ItemStyle-HorizontalAlign="left"
ItemStyle-Width="45%" />

<asp:BoundField DataField="LastWriteTime"
HeaderText="Last Write Time"
SortExpression="LastWriteTime"
HeaderStyle-VerticalAlign="Bottom"
ItemStyle-HorizontalAlign="left"
ItemStyle-Width="40%" />

<asp:BoundField DataField="Length"
HeaderText="File Size"
ReadOnly="True"
SortExpression="Length"
HeaderStyle-VerticalAlign="Bottom"
ItemStyle-HorizontalAlign="center"
ItemStyle-Width="15%" />

</Columns>
</asp:GridView>



protected void Search_Click(object Sender, CommandEventArgs Args)
{
this.DirectoryList(this.txtSearch.Text);
}


protected void DirectoryList(string DirectoryPath)
{
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(str00));
this.gvSearchList.DataSource = dirInfo.GetFiles("*.*");
this.gvSearchList.DataBind();
}


protected void gvSearchList_SelectedIndexChanged(object Sender, EventArgs
e)
{

}


private string ConvertSortDirectionToSql(SortDirection sortDirection)
{
string newSortDirection = String.Empty;

switch (sortDirection)
{
case SortDirection.Ascending:
newSortDirection = "ASC";
break;

case SortDirection.Descending:
newSortDirection = "DESC";
break;
}

return newSortDirection;
}


protected void gvSearchList_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
gvSearchList.PageIndex = e.NewPageIndex;
gvSearchList.DataBind();
this.gvSearchList.Visible = true;
}


protected void gvSearchList_Sorting(object sender, GridViewSortEventArgs
e)
{
DataTable dataTable = gvSearchList.DataSource as DataTable;

if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
dataView.Sort = e.SortExpression + " " +
ConvertSortDirectionToSql(e.SortDirection);

gvSearchList.DataSource = dataView;
gvSearchList.DataBind();
}
}
 
W

Walter Wang [MSFT]

Hi,

I suggest you create a DataSource (with its DataView) to support pagination
and sorting.

Please take a look at following article:

#Folder Contents DataSource Control - The Code Project - ASP.NET
http://www.codeproject.com/useritems/FolderContentsDataSource.asp


I think it's what you needed. Let me know if it's not the case.


Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top