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="gvSearchLis t" runat="server"
AllowPaging="Tr ue"
AllowSorting="T rue"
AutoGenerateSel ectButton="Fals e"
AutoGenerateCol umns="False"
PageSize="15"
style="width:70 %"
OnPageIndexChan ging="gvSearchL ist_PageIndexCh anging"
OnSorting="gvSe archList_Sortin g"
OnSelectedIndex Changed="gvSear chList_Selected IndexChanged">
<Columns>
<asp:BoundFie ld DataField="Name "
HeaderText="Fil e Name"
SortExpression= "Name"
HeaderStyle-VerticalAlign=" Bottom"
ItemStyle-HorizontalAlign ="left"
ItemStyle-Width="45%" />
<asp:BoundFie ld DataField="Last WriteTime"
HeaderText="Las t Write Time"
SortExpression= "LastWriteT ime"
HeaderStyle-VerticalAlign=" Bottom"
ItemStyle-HorizontalAlign ="left"
ItemStyle-Width="40%" />
<asp:BoundFie ld DataField="Leng th"
HeaderText="Fil e Size"
ReadOnly="True"
SortExpression= "Length"
HeaderStyle-VerticalAlign=" Bottom"
ItemStyle-HorizontalAlign ="center"
ItemStyle-Width="15%" />
</Columns>
</asp:GridView>
protected void Search_Click(ob ject Sender, CommandEventArg s Args)
{
this.DirectoryL ist(this.txtSea rch.Text);
}
protected void DirectoryList(s tring DirectoryPath)
{
DirectoryInfo dirInfo = new DirectoryInfo(S erver.MapPath(s tr00));
this.gvSearchLi st.DataSource = dirInfo.GetFile s("*.*");
this.gvSearchLi st.DataBind();
}
protected void gvSearchList_Se lectedIndexChan ged(object Sender, EventArgs
e)
{
}
private string ConvertSortDire ctionToSql(Sort Direction sortDirection)
{
string newSortDirectio n = String.Empty;
switch (sortDirection)
{
case SortDirection.A scending:
newSortDirectio n = "ASC";
break;
case SortDirection.D escending:
newSortDirectio n = "DESC";
break;
}
return newSortDirectio n;
}
protected void gvSearchList_Pa geIndexChanging (object sender,
GridViewPageEve ntArgs e)
{
gvSearchList.Pa geIndex = e.NewPageIndex;
gvSearchList.Da taBind();
this.gvSearchLi st.Visible = true;
}
protected void gvSearchList_So rting(object sender, GridViewSortEve ntArgs
e)
{
DataTable dataTable = gvSearchList.Da taSource as DataTable;
if (dataTable != null)
{
DataView dataView = new DataView(dataTa ble);
dataView.Sort = e.SortExpressio n + " " +
ConvertSortDire ctionToSql(e.So rtDirection);
gvSearchList.Da taSource = dataView;
gvSearchList.Da taBind();
}
}