Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > ASP.NET > ASP.NET GridView Control > ASP.NET GridView Control - AllowSorting and AllowPaging



Clound SSD Virtual Server

ASP.NET GridView Control - AllowSorting and AllowPaging

ASP.NET GridView Control - AllowSorting and AllowPaging ตัวอย่างนี้ผมได้ยากตัวอย่างการใช้ GridView เพื่อทำการแบ่งหน้าและโหมดในส่วนของการจัดเรียงพร้อมๆ กัน

Language Code : VB.NET || C#

Framework : 2,3,4


GridViewAllowSortingAllowPaging.aspx

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">
	Dim strFields As String = "CustomerID"
    Sub Page_Load(sender As Object, e As EventArgs)		
		IF Not Page.IsPostBack() Then
			BindData()
		End IF
    End Sub

	Sub BindData()

        Dim objConn As New OleDbConnection
		Dim objCmd As New OleDbCommand
        Dim dtAdapter As New OleDbDataAdapter       
		Dim ds As New DataSet
		Dim strConnString,strSQL As String

		strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& _
		Server.MapPath("database/mydatabase.mdb")&";"
		strSQL = "SELECT * FROM customer ORDER BY " & strFields & " ASC"

		objConn.ConnectionString = strConnString
		With objCmd
			.Connection = objConn
			.CommandText = strSQL			
			.CommandType = CommandType.Text
		End With
		dtAdapter.SelectCommand = objCmd

		dtAdapter.Fill(ds)

		'*** BindData to GridView ***'
		myGridView.DataSource = ds
		myGridView.DataBind()

		dtAdapter = Nothing		
		objConn.Close()
		objConn = Nothing

	End Sub
	
	Sub ShowPageCommand(s As Object, e As GridViewPageEventArgs)
		myGridView.PageIndex = e.NewPageIndex
		BindData()
	End Sub	

	Sub SortCommand(s As Object, e As GridViewSortEventArgs)
		strFields = e.SortExpression
		BindData()
	End Sub


	Private Sub myGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs) 
		'*** CustomerID ***'
		Dim lblCustomerID As Label = CType(e.Row.FindControl("lblCustomerID"),Label)
		IF Not IsNothing(lblCustomerID) Then
			lblCustomerID.Text = e.Row.DataItem("CustomerID")
		End IF

		'*** Email ***'
		Dim lblName As Label = CType(e.Row.FindControl("lblName"),Label)
		IF Not IsNothing(lblName) Then
			lblName.Text = e.Row.DataItem("Name")
		End IF

		'*** Name ***'
		Dim lblEmail As Label = CType(e.Row.FindControl("lblEmail"),Label)
		IF Not IsNothing(lblEmail) Then
			lblEmail.Text = e.Row.DataItem("Email")
		End IF

		'*** CountryCode ***'
		Dim lblCountryCode As Label = CType(e.Row.FindControl("lblCountryCode"),Label)
		IF Not IsNothing(lblCountryCode) Then
			lblCountryCode.Text = e.Row.DataItem("CountryCode")
		End IF

		'*** Budget ***'
		Dim lblBudget As Label = CType(e.Row.FindControl("lblBudget"),Label)
		IF Not IsNothing(lblBudget) Then
			lblBudget.Text = FormatNumber(e.Row.DataItem("Budget"),2)
		End IF

		'*** Used ***'
		Dim lblUsed As Label = CType(e.Row.FindControl("lblUsed"),Label)
		IF Not IsNothing(lblUsed) Then
			lblUsed.Text = FormatNumber(e.Row.DataItem("Used"),2)
		End IF
	End Sub

</script>
<html>
<head>
    <title>ThaiCreate.Com ASP.NET - GridView</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView id="myGridView" runat="server"  PageSize="2" OnSorting="SortCommand"
	OnPageIndexChanging="ShowPageCommand" AllowPaging="True"  AllowSorting="True"
	AutoGenerateColumns="False" onRowDataBound="myGridView_RowDataBound">

	<Columns>

	<asp:TemplateField SortExpression="CustomerID"  HeaderText="CustomerID">
		<ItemTemplate>
			<asp:Label id="lblCustomerID" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="Name" HeaderText="Name">
		<ItemTemplate>
			<asp:Label id="lblName" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="Email" HeaderText="Email">
		<ItemTemplate>
			<asp:Label id="lblEmail" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="CountryCode" HeaderText="CountryCode">
		<ItemTemplate>
			<asp:Label id="lblCountryCode" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="Budget" HeaderText="Budget">
		<ItemTemplate>
			<asp:Label id="lblBudget" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	<asp:TemplateField SortExpression="Used" HeaderText="Used">
		<ItemTemplate>
			<asp:Label id="lblUsed" runat="server"></asp:Label>
		</ItemTemplate>
	</asp:TemplateField>

	</Columns>
</asp:GridView>
</form>
</body>
</html>









Screenshot

ASP.NET GridView Control

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2008-11-13 07:19:17 / 2017-03-28 21:18:57
  Download : Download  ASP.NET GridView Control - AllowSorting and AllowPaging
 Sponsored Links / Related

 
ASP.NET GridView Control
Rating :

 
ASP.NET GridView Control - DataBind
Rating :

 
ASP.NET GridView Control - AlternateRows
Rating :

 
ASP.NET GridView Control - TemplateField
Rating :

 
ASP.NET GridView Control - RowDataBound
Rating :

 
ASP.NET GridView Control - AllowSorting
Rating :

 
ASP.NET GridView Control - AllowPaging
Rating :

 
ASP.NET GridView Control - FindControl
Rating :

 
ASP.NET GridView Control - DataSet,DataTable,TableRows
Rating :

 
ASP.NET GridView Control - RowCommand
Rating :

 
ASP.NET GridView Control - DropDownList in GridView
Rating :

 
ASP.NET GridView Control - XML
Rating :

 
ASP.NET GridView Control - AccessDataSource
Rating :

 
ASP.NET GridView Control - VS 2005,2008,2010 (FX 2.0,3.5,4.0)
Rating :

 
ASP.NET GridView Control - Microsoft Access (.mdb) - System.Data.OleDb
Rating :

 
ASP.NET GridView Control - SQL Server 2000,2005,2008 - System.Data.SqlClient
Rating :

 
ASP.NET GridView Control - MySQL Database - MySql.Data.MySqlClient
Rating :

 
ASP.NET GridView Control - Oracle Database - System.Data.OracleClient
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 02
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่