This is example of how to enable Sorting In GridView By Clicking on Columns Or Headers In Ascending Or Descending Direction In Asp.Net Using C# And VB
If you have Populated GridView With ObjectDataSource or SqlDataSOurce to Insert Update Edit Delete record , then You just have toset AllowSorting property to True andSortExpression property of columns to respective field from database to sort.
Setting these properties changes column names to hyperlink and No coding is required.
Write html source as mentioned below
IF SQLCONNECTION AND SQLCOMMAND IS USED IN CODE BEHIND
If data is populated from database using code behind, then to sortwe need to write custom codeto save direction (Ascending or Descending) in ViewState and writecode in Sorting Event of Grid.
Populate and load GridView on Page_Load Event
BindGridView Method fetches data from database and return it as DataTable.
C# CODE
VB.NET CODE
Create Public Property of SortDirection type and store direction in ViewState.
C# CODE
VB.NET CODE
Check Gridview's current direction from ViewState and set new sort direction in Sorting Event.
C# CODE
VB.NET CODE
If you have Populated GridView With ObjectDataSource or SqlDataSOurce to Insert Update Edit Delete record , then You just have toset AllowSorting property to True andSortExpression property of columns to respective field from database to sort.
Setting these properties changes column names to hyperlink and No coding is required.
Write html source as mentioned below
<asp:GridView ID="gvDetails" runat="server"
AutoGenerateColumns="False"
onsorting="gvDetails_Sorting">
<Columns>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFname" runat="server" Text='<%#Eval("FirstName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%#Eval("LastName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
IF SQLCONNECTION AND SQLCOMMAND IS USED IN CODE BEHIND
If data is populated from database using code behind, then to sortwe need to write custom codeto save direction (Ascending or Descending) in ViewState and writecode in Sorting Event of Grid.
Populate and load GridView on Page_Load Event
BindGridView Method fetches data from database and return it as DataTable.
C# CODE
01
protected
void
Page_Load(
object
sender, EventArgs e)
02
{
03
if
(!Page.IsPostBack)
04
{
05
gvDetails.DataSource = BindGridView();
06
gvDetails.DataBind();
07
}
08
}
09
10
private
DataTable BindGridView()
11
{
12
DataTable dtGrid =
new
DataTable();
13
SqlConnection con =
new
SqlConnection(ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString);
14
string
strSelect =
"SELECT FirstName,LastName,Location FROM Details"
;
15
SqlCommand cmd =
new
SqlCommand(strSelect, con);
16
SqlDataAdapter dAdapter =
new
SqlDataAdapter(cmd);
17
dAdapter.Fill(dtGrid);
18
return
dtGrid;
19
}
VB.NET CODE
01
Protected
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
02
If
Not
Page.IsPostBack
Then
03
gvDetails.DataSource = BindGridView()
04
gvDetails.DataBind()
05
End
If
06
End
Sub
07
08
Private
Function
BindGridView()
As
DataTable
09
Dim
dtGrid
As
New
DataTable()
10
Dim
con
As
New
SqlConnection(ConfigurationManager.ConnectionStrings(
"ConnectionString"
).ConnectionString)
11
Dim
strSelect
As
String
=
"SELECT FirstName,LastName,Location FROM Details"
12
Dim
cmd
As
New
SqlCommand(strSelect, con)
13
Dim
dAdapter
As
New
SqlDataAdapter(cmd)
14
dAdapter.Fill(dtGrid)
15
Return
dtGrid
16
End
Function
Create Public Property of SortDirection type and store direction in ViewState.
C# CODE
01
public
SortDirection dir
02
{
03
get
04
{
05
if
(ViewState[
"dirState"
] ==
null
)
06
{
07
ViewState[
"dirState"
] = SortDirection.Ascending;
08
}
09
return
(SortDirection)ViewState[
"dirState"
];
10
}
11
set
12
{
13
ViewState[
"dirState"
] = value;
14
}
15
}
VB.NET CODE
01
Public
Property
dir()
As
SortDirection
02
Get
03
If
ViewState(
"dirState"
)
Is
Nothing
Then
04
ViewState(
"dirState"
) = SortDirection.Ascending
05
End
If
06
Return
DirectCast
(ViewState(
"dirState"
), SortDirection)
07
End
Get
08
Set
09
ViewState(
"dirState"
) = value
10
End
Set
11
End
Property
Check Gridview's current direction from ViewState and set new sort direction in Sorting Event.
C# CODE
01
protected
void
gvDetails_Sorting(
object
sender, GridViewSortEventArgs e)
02
{
03
string
sortingDirection =
string
.Empty;
04
if
(dir == SortDirection.Ascending)
05
{
06
dir = SortDirection.Descending;
07
sortingDirection =
"Desc"
;
08
}
09
else
10
{
11
dir = SortDirection.Ascending;
12
sortingDirection =
"Asc"
;
13
}
14
15
DataView sortedView =
new
DataView(BindGridView());
16
sortedView.Sort = e.SortExpression +
" "
+ sortingDirection;
17
gvDetails.DataSource = sortedView;
18
gvDetails.DataBind();
19
}
VB.NET CODE
01
Protected
Sub
gvDetails_Sorting(sender
As
Object
, e
As
GridViewSortEventArgs)
02
Dim
sortingDirection
As
String
=
String
.Empty
03
If
dir = SortDirection.Ascending
Then
04
dir = SortDirection.Descending
05
sortingDirection =
"Desc"
06
Else
07
dir = SortDirection.Ascending
08
sortingDirection =
"Asc"
09
End
If
10
11
Dim
sortedView
As
New
DataView(BindGridView())
12
sortedView.Sort = Convert.ToString(e.SortExpression) &
" "
& sortingDirection
13
gvDetails.DataSource = sortedView
14
gvDetails.DataBind()
15
End
Sub
0 comments:
Post a Comment