sorting problem!

R

ricky

hello all,
how can i modify my code to make my datagrid sortable??please help


'code behind
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Data.SqlClient
Imports System.Windows.Forms.Form

Public Class WebForm4
Inherits System.Web.UI.Page
'Inherits DataGridTextBoxColumn

Protected WithEvents ButtonAdd As System.Web.UI.WebControls.Button
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents TxtCiano As System.Web.UI.WebControls.TextBox
Protected WithEvents TxtEmail As System.Web.UI.WebControls.TextBox
Protected WithEvents NavigateType As
System.Web.UI.WebControls.RadioButtonList

Dim CartView As DataView

Dim mySqlConn As SqlConnection
Dim myCommand As SqlCommand
Dim myReader As SqlDataReader
Dim dt As DataTable
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents lblMessage As System.Web.UI.WebControls.Label
Protected WithEvents SqlDataAdapter1 As
System.Data.SqlClient.SqlDataAdapter
Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Protected WithEvents DSmail1 As vbnet_web_adonet.DSmail
Protected WithEvents DataGrid1 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

Dim dr As DataRow
Dim queryType As String

#Region " Web Form 設計工具產生的程å¼ç¢¼ "

'此呼å«ç‚º Web Form 設計工具的必è¦é …。
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
Me.DSmail1 = New vbnet_web_adonet.DSmail()
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()
CType(Me.DSmail1,
System.ComponentModel.ISupportInitialize).BeginInit()
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.DeleteCommand = Me.SqlDeleteCommand1
Me.SqlDataAdapter1.InsertCommand = Me.SqlInsertCommand1
Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
Me.SqlDataAdapter1.TableMappings.AddRange(New
System.Data.Common.DataTableMapping() {New
System.Data.Common.DataTableMapping("Table", "mail", New
System.Data.Common.DataColumnMapping() {New
System.Data.Common.DataColumnMapping("id", "id"), New
System.Data.Common.DataColumnMapping("ciano", "ciano"), New
System.Data.Common.DataColumnMapping("email", "email")})})
Me.SqlDataAdapter1.UpdateCommand = Me.SqlUpdateCommand1
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "data source=RICKYNB;initial
catalog=FFBC;persist security info=False;user id=sa;w" & _
"orkstation id=RICKYNB;packet size=4096"
'
'DSmail1
'
Me.DSmail1.DataSetName = "DSmail"
Me.DSmail1.Locale = New System.Globalization.CultureInfo("zh-TW")
Me.DSmail1.Namespace = "http://www.tempuri.org/DSmail.xsd"
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT id, ciano, email FROM mail"
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
'
'SqlInsertCommand1
'
Me.SqlInsertCommand1.CommandText = "INSERT INTO mail(ciano, email)
VALUES (@ciano, @email); SELECT id, ciano, email F" & _
"ROM mail WHERE (id = @@IDENTITY)"
Me.SqlInsertCommand1.Connection = Me.SqlConnection1
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@ciano", System.Data.SqlDbType.VarChar,
50, "ciano"))
Me.SqlInsertCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@email", System.Data.SqlDbType.VarChar,
100, "email"))
'
'SqlUpdateCommand1
'
Me.SqlUpdateCommand1.CommandText = "UPDATE mail SET ciano = @ciano,
email = @email WHERE (id = @Original_id) AND (cia" & _
"no = @Original_ciano) AND (email = @Original_email); SELECT id,
ciano, email FRO" & _
"M mail WHERE (id = @id)"
Me.SqlUpdateCommand1.Connection = Me.SqlConnection1
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@ciano", System.Data.SqlDbType.VarChar,
50, "ciano"))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@email", System.Data.SqlDbType.VarChar,
100, "email"))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_id", System.Data.SqlDbType.Int,
4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0,
Byte), "id", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_ciano",
System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "ciano",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_email",
System.Data.SqlDbType.VarChar, 100, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "email",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@id", System.Data.SqlDbType.Int, 4, "id"))
'
'SqlDeleteCommand1
'
Me.SqlDeleteCommand1.CommandText = "DELETE FROM mail WHERE (id =
@Original_id) AND (ciano = @Original_ciano) AND (ema" & _
"il = @Original_email)"
Me.SqlDeleteCommand1.Connection = Me.SqlConnection1
Me.SqlDeleteCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_id", System.Data.SqlDbType.Int,
4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0,
Byte), "id", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_ciano",
System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "ciano",
System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New
System.Data.SqlClient.SqlParameter("@Original_email",
System.Data.SqlDbType.VarChar, 100, System.Data.ParameterDirection.Input,
False, CType(0, Byte), CType(0, Byte), "email",
System.Data.DataRowVersion.Original, Nothing))
CType(Me.DSmail1, System.ComponentModel.ISupportInitialize).EndInit()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法呼å«ç‚º Web Form 設計工具的必è¦é …
'請勿使用程å¼ç¢¼ç·¨è¼¯å™¨ä¾†ä¿®æ”¹å®ƒã€‚
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
BindGrid()
End If
End Sub



Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonAdd.Click

If Trim(TxtCiano.Text) = "" Or Trim(TxtEmail.Text) = "" Then

Else
Dim sqlcmd = "INSERT INTO mail (ciano, email)VALUES('" &
Trim(TxtCiano.Text) & "','" & Trim(TxtEmail.Text) & "') "
connBySql(sqlcmd)
myCommand.ExecuteNonQuery()
mySqlConn.Close()
Page_Load(sender, e)
TxtCiano.Text = ""
TxtEmail.Text = ""
BindGrid()
End If
End Sub




Sub BindGrid()

'lblMessage.Text = Me.SqlSelectCommand1.CommandText
lblMessage.Text = "queryType" & queryType

DataGrid1.DataKeyField() = "id"
Me.SqlDataAdapter1.Fill(DSmail1)
Me.DataGrid1.DataBind()
End Sub



Private Sub connBySql(ByVal sqlStr As String)
mySqlConn = New SqlConnection(conn.connectsql)
myCommand = New SqlCommand(sqlStr, mySqlConn)
mySqlConn.Open()

End Sub


Private Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles
DataGrid1.SortCommand
BindGridToSource2(e.SortExpression)

'DataGrid1.CurrentPageIndex = 0

''得到排åºçš„列

'ViewState("strSort") = e.SortExpression.ToString()

'DataGrid1.DataSource = GetDv(ViewState("strSort").ToString())

'DataGrid1.DataBind()


End Sub

Private Sub BindGridToSource()
DataGrid1.PagerStyle.Mode = PagerMode.NextPrev
BindGridToSource()
End Sub

Private Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End Sub

Private Sub BindGridToSource2(ByVal SortField As String)
'queryType = SortField
'Me.SqlSelectCommand1.CommandText = "SELECT id, ciano, email FROM
mail order by " & SortField & " asc"

'BindGrid()
Dim source As DataView = DSmail1.Tables("mail").DefaultView
source.Sort = SortField
BindGrid()
End Sub




End Class


'mail.aspx
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="mail.aspx.vb"
Inherits="vbnet_web_adonet.WebForm4"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>mail</title>
<meta content="Microsoft Visual Studio .NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="新細明體">
<asp:label id="Label2" style="Z-INDEX: 106; LEFT: 131px; POSITION:
absolute; TOP: 103px" runat="server" Height="30px" Width="111px">E-mail
Address</asp:label><asp:button id="ButtonAdd" style="Z-INDEX: 102; LEFT:
131px; POSITION: absolute; TOP: 147px" runat="server" Height="28px"
Width="92px" Text="新增"></asp:button><asp:textbox id="TxtCiano"
style="Z-INDEX: 103; LEFT: 245px; POSITION: absolute; TOP: 69px"
runat="server" Height="26px" Width="212px"></asp:textbox><asp:textbox
id="TxtEmail" style="Z-INDEX: 104; LEFT: 245px; POSITION: absolute; TOP:
105px" runat="server" Height="26px" Width="214px"></asp:textbox><asp:label
id="Label1" style="Z-INDEX: 105; LEFT: 129px; POSITION: absolute; TOP: 72px"
runat="server" Height="30px" Width="113px">CIA #</asp:label><asp:label
id="Label3" style="Z-INDEX: 107; LEFT: 244px; POSITION: absolute; TOP: 23px"
runat="server" Height="25px" Width="272px">客戶E-mail 管ç†</asp:label><asp:label
id="lblMessage" style="Z-INDEX: 108; LEFT: 419px; POSITION: absolute; TOP:
146px" runat="server" Height="30px" Width="104px"></asp:label><asp:datagrid
id=DataGrid1 style="Z-INDEX: 109; LEFT: 123px; POSITION: absolute; TOP:
190px" runat="server" Height="340px" Width="563px" DataKeyField="ciano"
AutoGenerateColumns="False" DataMember="mail" DataSource="<%# DSmail1 %>"
AllowPaging="True" AllowSorting="True" BorderColor="#0000CC"
BorderWidth="3px">
<HeaderStyle ForeColor="Black" BackColor="#3399CC"></HeaderStyle>
<Columns>
<asp:TemplateColumn></asp:TemplateColumn>
<asp:EditCommandColumn ButtonType="PushButton" UpdateText="æ›´æ–°"
HeaderText="編輯 å–消" CancelText="å–消" EditText="編輯"></asp:EditCommandColumn>
<asp:ButtonColumn Text="刪除" ButtonType="PushButton" HeaderText="刪除"
CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="id" SortExpression="id" ReadOnly="True"
HeaderText="id">
<HeaderStyle Width="20px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="ciano" SortExpression="ciano"
HeaderText="ciano">
<HeaderStyle Height="20px" Width="180px"></HeaderStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="email" SortExpression="email"
HeaderText="email">
<HeaderStyle Width="180px"></HeaderStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle NextPageText="下一é " PrevPageText="上一é "
HorizontalAlign="Center" Position="TopAndBottom"
Mode="NumericPages"></PagerStyle>
</asp:datagrid></FONT></form>
</body>
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,215
Messages
2,571,113
Members
47,716
Latest member
MiloManley

Latest Threads

Top