Hi Mark,
Thanks for your response and the detailed code you provided. After reading
your code , I think you may have a check on the following point, your
grid's SortComand handler and binddata function is as below:
===================================
Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
DataSet11.Table1.DefaultView.Sort = e.SortExpression
Z_BindDataGrid()
End Sub
Private Sub Z_BindDataGrid()
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataBind()
End Sub
=====================================
that means, you do the following things when do sorting:
1. set the datatable's DefaultView's Sort property (this is the most
important for sortin)
2. Fill the dataset again from the database via dataadapter
3. bind the default view to datagrid.
I suggest that you change it to the following order:
1. Fill the dataset again from database
2. set the datatable's defaultview's "Sort" property( after refilling the
data from dataadapter)
3.Bind to the datagrid.
In addition, I've also done a simple test on my side use the "PUBS" SAMPLE
database in sqlserver , I also use the wizard in VS.NET to generate the
connection and dataadapter( drag a table,some specified columns by CTRL+
right click the wanted columns), but I use a untyped dataset, the test page
code is as below, you may also have a test on your side if you feel
necessary:
#(Do remember to change the connection string regarding on the database
server address on your side
)
=============aspx page=================
<HTML>
<HEAD>
<title>SortingGrid</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="
http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td>
<asp
ataGrid id="dgSort" runat="server" AllowSorting="True"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="title_id" SortExpression="title_id"
HeaderText="TitleId"></asp:BoundColumn>
<asp:BoundColumn DataField="title" SortExpression="title"
HeaderText="Title"></asp:BoundColumn>
<asp:BoundColumn DataField="type" SortExpression="type"
HeaderText="Type"></asp:BoundColumn>
<asp:BoundColumn DataField="pub_id" SortExpression="pub_id"
HeaderText="PubId"></asp:BoundColumn>
<asp:BoundColumn DataField="price" SortExpression="price"
HeaderText="Price"></asp:BoundColumn>
</Columns>
</asp
ataGrid></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
==========code behind======================
Public Class SortingGrid
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlInsertCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlUpdateCommand1 = New System.Data.SqlClient.SqlCommand
Me.SqlDeleteCommand1 = New System.Data.SqlClient.SqlCommand
Me.conn = New System.Data.SqlClient.SqlConnection
Me.adapter = New System.Data.SqlClient.SqlDataAdapter
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT title_id, title, type,
pub_id, price FROM titles"
Me.SqlSelectCommand1.Connection = Me.conn
'
'SqlInsertCommand1
'
Me.SqlInsertCommand1.CommandText = "INSERT INTO titles(title_id,
title, type, pub_id, price) VALUES (@title_id, @titl" & _
"e, @type, @pub_id,
@Price); SELECT title_id, title, type, pub_id,
price FROM tit" & _
"les WHERE (title_id = @title_id)"
Me.SqlInsertCommand1.Connection = Me.conn
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@title_id",
System.Data.SqlDbType.VarChar, 6, "title_id"))
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@title", System.Data.SqlDbType.VarChar,
80, "title"))
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@type", System.Data.SqlDbType.VarChar,
12, "type"))
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@pub_id",
System.Data.SqlDbType.VarChar, 4, "pub_id"))
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("
@Price", System.Data.SqlDbType.Money,
8, "price"))
'
'SqlUpdateCommand1
'
Me.SqlUpdateCommand1.CommandText = "UPDATE titles SET title_id =
@title_id, title = @title, type = @type, pub_id = @p" & _
"ub_id, price =
@Price WHERE (title_id = @Original_title_id) AND
(price = @Origin" & _
"al_price OR @Original_price IS NULL AND price IS NULL) AND (pub_id
= @Original_p" & _
"ub_id OR @Original_pub_id IS NULL AND pub_id IS NULL) AND (title =
@Original_tit" & _
"le) AND (type = @Original_type); SELECT title_id, title, type,
pub_id, price FRO" & _
"M titles WHERE (title_id = @title_id)"
Me.SqlUpdateCommand1.Connection = Me.conn
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@title_id",
System.Data.SqlDbType.VarChar, 6, "title_id"))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@title", System.Data.SqlDbType.VarChar,
80, "title"))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@type", System.Data.SqlDbType.VarChar,
12, "type"))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@pub_id",
System.Data.SqlDbType.VarChar, 4, "pub_id"))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("
@Price", System.Data.SqlDbType.Money,
8, "price"))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_title_id",
System.Data.SqlDbType.VarChar, 6, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "title_id",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_price",
System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "price",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_pub_id",
System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "pub_id",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_title",
System.Data.SqlDbType.VarChar, 80, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "title",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_type",
System.Data.SqlDbType.VarChar, 12, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "type",
System.Data.DataRowVersion.Original, Nothing))
'
'SqlDeleteCommand1
'
Me.SqlDeleteCommand1.CommandText = "DELETE FROM titles WHERE
(title_id = @Original_title_id) AND (price = @Original_p" & _
"rice OR @Original_price IS NULL AND price IS NULL) AND (pub_id =
@Original_pub_i" & _
"d OR @Original_pub_id IS NULL AND pub_id IS NULL) AND (title =
@Original_title) " & _
"AND (type = @Original_type)"
Me.SqlDeleteCommand1.Connection = Me.conn
Me.SqlDeleteCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_title_id",
System.Data.SqlDbType.VarChar, 6, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "title_id",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_price",
System.Data.SqlDbType.Money, 8, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "price",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_pub_id",
System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "pub_id",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_title",
System.Data.SqlDbType.VarChar, 80, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "title",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_type",
System.Data.SqlDbType.VarChar, 12, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "type",
System.Data.DataRowVersion.Original, Nothing))
'
'conn
'
'#####-----------------------------------------------#####
'##### REPLACE THE CONNECTIONSTRING WITH YOUR OWN #####
'#####-----------------------------------------------#####
Me.conn.ConnectionString = "workstation id=""SHA-SCHANG-01"";packet
size=4096;user id=s;integrated security=SSP" & _
"I;data source=""(local)"";persist security info=False;initial
catalog=pubs"
'
'adapter
'
Me.adapter.DeleteCommand = Me.SqlDeleteCommand1
Me.adapter.InsertCommand = Me.SqlInsertCommand1
Me.adapter.SelectCommand = Me.SqlSelectCommand1
Me.adapter.TableMappings.AddRange(New
System.Data.Common.DataTableMapping() {New
System.Data.Common.DataTableMapping("Table", "titles", New
System.Data.Common.DataColumnMapping() {New
System.Data.Common.DataColumnMapping("title_id", "title_id"), New
System.Data.Common.DataColumnMapping("title", "title"), New
System.Data.Common.DataColumnMapping("type", "type"), New
System.Data.Common.DataColumnMapping("pub_id", "pub_id"), New
System.Data.Common.DataColumnMapping("price", "price")})})
Me.adapter.UpdateCommand = Me.SqlUpdateCommand1
End Sub
Protected WithEvents dgSort As System.Web.UI.WebControls.DataGrid
Protected WithEvents SqlSelectCommand1 As
System.Data.SqlClient.SqlCommand
Protected WithEvents SqlInsertCommand1 As
System.Data.SqlClient.SqlCommand
Protected WithEvents SqlUpdateCommand1 As
System.Data.SqlClient.SqlCommand
Protected WithEvents SqlDeleteCommand1 As
System.Data.SqlClient.SqlCommand
Protected WithEvents conn As System.Data.SqlClient.SqlConnection
Protected WithEvents adapter As System.Data.SqlClient.SqlDataAdapter
'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim ds As DataSet = GetDataSource()
dgSort.DataSource = ds.Tables(0).DefaultView
dgSort.DataBind()
End If
End Sub
Protected Function GetDataSource() As DataSet
Dim ds As New DataSet
Me.adapter.Fill(ds)
Return ds
End Function
Private Sub dgSort_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
dgSort.SortCommand
Dim ds As DataSet = GetDataSource()
Dim dt As DataTable = ds.Tables(0)
dt.DefaultView.Sort = e.SortExpression
dgSort.DataSource = dt.DefaultView
dgSort.DataBind()
End Sub
End Class
===========================================
Thanks.
Regards,
Steven Cheng
Microsoft Online Support
Get Secure!
www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx