how to sort the gridview values in vb.net? [Resolved]

Posted by Hariinakoti under VB.NET on 11/1/2012 | Points: 10 | Views : 6314 | Status : [Member] | Replies : 2
Hi All,
How to sort the gridview values in vb.net .I selected sorting property is true.But it is not working.In my GridView temporary values r there.
I'm using this code for reading folder and display that values in gridview it is working fine.Now i want to sort that values based on file creation date.

Code is:
Dim dir As New DirectoryInfo("E:\Sample")
GridView1.DataSource = dir.GetFiles()
GridView1.DataBind()

Please reply asap

Thanks & Regards
Hari



Responses

Posted by: Hariinakoti on: 11/7/2012 [Member] Starter | Points: 25

Up
0
Down

Resolved
I got solution
In source:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">
<RowStyle BackColor="White" ForeColor="#003399" />
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Click Here" BackColor="#CCCCFF" ForeColor="#003399" Font-Bold="True" />
<asp:Button ID="Button2" runat="server" Text="Clear" BackColor="#CCCCFF" ForeColor="#003399" Font-Bold="True"/>
</div>
</form>
</body>
</html>


back end:

Imports System.IO
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim objFSO = Server.CreateObject("Scripting.FileSystemObject")
Dim objFolder = objFSO.GetFolder("E:\Sample")
Dim dt As New DataTable
Dim FileName As DataColumn = New DataColumn("File Name")
FileName.DataType = System.Type.GetType("System.String")
Dim DateModified As DataColumn = New DataColumn("Date Modified")
DateModified.DataType = System.Type.GetType("System.DateTime")
dt.Columns.Add(FileName)
dt.Columns.Add(DateModified)
Dim objFile
For Each objFile In objFolder.Files
Dim dr As DataRow
dr = dt.NewRow()
dr.Item("File Name") = objFile.Name
dr.Item("Date Modified") = objFile.DateLastModified
dt.Rows.Add(dr)
Next
Dim dv As DataView
dv = New DataView
dv = dt.DefaultView
dv.Sort = "Date Modified"
GridView1.DataSource = dt
GridView1.DataBind()
objFolder = Nothing
objFSO = Nothing
GridView1.Visible = True
Button1.Visible = False
Button2.Visible = True
End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Button1.Visible = True
GridView1.Visible = False
End Sub

Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
Button1.Visible = True
Button2.Visible = False
End Sub
End Class


Thanks & Regards
Hari

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajesh081725 on: 11/5/2012 [Member] Bronze | Points: 25

Up
0
Down
For sorting you have to bind the grid...



private void LoadGrid(string sortExpr, string sortDirection)
{


DataSet ds = new DataSet();
SqlDataAdapter SqlDA = new SqlDataAdapter("Select * from tablename order by "+sortExpr+" "+sortDirection+"", connectionString);
SqlDA.Fill(ds);
if (ds != null)
{
gdManageForm.DataSource = ds;
gdManageForm.EditIndex = -1;
gdManageForm.DataBind();
}

}

protected void gdManageForm_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
ViewState["SortExpression"] = sortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}

private void SortGridView(string sortExpression, string direction)
{
//If it is default state
LoadGrid(sortExpression, direction);
}

private SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}


Time is Gold
Thanks & Regards,
Rajesh Kumar,
9962038582.

Hariinakoti, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response