rawdrib
asked on
column sorting of datagrid
in the table that is returned from a SQL query, i want to be able to sort the columns. i have the AllowSorting=True, OnSortCommand="SortEventHa ndler", and the following HTML:
<Columns>
<asp:ButtonColumn Text="Details" HeaderText="Error Details"></asp:ButtonColum n>
<asp:BoundColumn Visible="False" DataField="ErrorID"></asp: BoundColum n>
<asp:BoundColumn Visible="False" DataField="CreateDate" SortExpression="E.CreateDa te DESC"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="ProcessOrPage"> </asp:Boun dColumn>
<asp:BoundColumn Visible="False" DataField="Error#" SortExpression="E.ErrorNum ber DESC"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="Status" SortExpression="ES.ErrorSt atus DESC"></asp:BoundColumn>
<asp:BoundColumn Visible="False" DataField="ErrorText"></as p:BoundCol umn>
</Columns>
The query joins two tables, E and ES. i use the fully qualified name for every attribute in the SQL command. The code behind is in VB and is as follows:
Public Sub SortEventHandler(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls. DataGridSo rtCommandE ventArgs)
SortEventHandlerBindData(e .SortExpre ssion)
End Sub
Public Sub SortEventHandlerBindData(B yVal sortExpr As String)
Dim myConnection As New _
SqlClient.SqlConnection(SQ LServerCon nection)
Dim strSQL As String = "SELECT E.ErrorID, E.CreateDate, ISNULL(E.PageURL,'')+ISNUL L(E.Proces sName,'') as ProcessOrPage, E.ErrorNumber as Error#, ES.ErrorDescription as Status, E.ErrorText FROM Error E join ErrorStatus ES on E.ErrorStatusID = ES.ErrorStatusID ORDER BY " & sortExpr
Dim myCommand As New SqlClient.SqlCommand(strSQ L, myConnection)
Dim myRdr As SqlClient.SqlDataReader
myConnection.Open()
myRdr = myCommand.ExecuteReader(Co mmandBehav ior.Defaul t)
Me.errorGrid.DataSource = myRdr
Me.errorGrid.DataBind()
End Sub
when the application is run, all the columns are able to be sorted except the CreateDate column. when attempted, it brings up the error:
Exception Details: System.Data.SqlClient.SqlE xception: Ambiguous column name 'CreateDate'.
Source Error:
Line 70:
Line 71: myConnection.Open()
Line 72: myRdr = myCommand.ExecuteReader(Co mmandBehav ior.Defaul t)
Line 73: Me.errorGrid.DataSource = myRdr
Line 74: Me.errorGrid.DataBind()
it seems to not be recognizing the qualified name of E.CreateDate. i have checked all the code and have not found a discrepency. any help is appreciated....thanks
<Columns>
<asp:ButtonColumn Text="Details" HeaderText="Error Details"></asp:ButtonColum
<asp:BoundColumn Visible="False" DataField="ErrorID"></asp:
<asp:BoundColumn Visible="False" DataField="CreateDate" SortExpression="E.CreateDa
<asp:BoundColumn Visible="False" DataField="ProcessOrPage">
<asp:BoundColumn Visible="False" DataField="Error#" SortExpression="E.ErrorNum
<asp:BoundColumn Visible="False" DataField="Status" SortExpression="ES.ErrorSt
<asp:BoundColumn Visible="False" DataField="ErrorText"></as
</Columns>
The query joins two tables, E and ES. i use the fully qualified name for every attribute in the SQL command. The code behind is in VB and is as follows:
Public Sub SortEventHandler(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.
SortEventHandlerBindData(e
End Sub
Public Sub SortEventHandlerBindData(B
Dim myConnection As New _
SqlClient.SqlConnection(SQ
Dim strSQL As String = "SELECT E.ErrorID, E.CreateDate, ISNULL(E.PageURL,'')+ISNUL
Dim myCommand As New SqlClient.SqlCommand(strSQ
Dim myRdr As SqlClient.SqlDataReader
myConnection.Open()
myRdr = myCommand.ExecuteReader(Co
Me.errorGrid.DataSource = myRdr
Me.errorGrid.DataBind()
End Sub
when the application is run, all the columns are able to be sorted except the CreateDate column. when attempted, it brings up the error:
Exception Details: System.Data.SqlClient.SqlE
Source Error:
Line 70:
Line 71: myConnection.Open()
Line 72: myRdr = myCommand.ExecuteReader(Co
Line 73: Me.errorGrid.DataSource = myRdr
Line 74: Me.errorGrid.DataBind()
it seems to not be recognizing the qualified name of E.CreateDate. i have checked all the code and have not found a discrepency. any help is appreciated....thanks
Try using the DataView control.
Dim dataView as New DataView(ds.Tables(0))
dataView.Sort = "[ColumnName] DESC" ' what you would put in ORDER BY in sql
DataGrid1.DataSource = dataView
DataGrid1.DataBind()
Dim dataView as New DataView(ds.Tables(0))
dataView.Sort = "[ColumnName] DESC" ' what you would put in ORDER BY in sql
DataGrid1.DataSource = dataView
DataGrid1.DataBind()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim authTicket As FormsAuthenticationTicket = CType(HttpContext.Current. User.Ident ity, FormsIdentity).Ticket()
HttpContext.Current.User = New GenericPrincipal(User.Iden tity, Split(authTicket.UserData, ","))
End Sub
Private Sub BindTheData(Optional ByVal LastName As String = "", Optional ByVal Phone As String = "")
Dim cnn As New SqlClient.SqlConnection(Co nfiguratio nSettings. AppSetting s("Emerald ConnStr"))
Dim cmd As New SqlClient.SqlCommand
Dim da As New SqlClient.SqlDataAdapter(c md)
Dim ds As New DataSet
cmd.CommandType = CommandType.StoredProcedur e
cmd.CommandText = "Miscellaneous.dbo.Contact RequestVie wAll"
cmd.Parameters.Add(New SqlParameter("@LastName", LastName))
cmd.Parameters.Add(New SqlParameter("@Phone", Phone))
cmd.Connection = cnn
cnn.Open()
da.SelectCommand = cmd
da.Fill(ds, "ContactRequests")
dg.DataSource = ds
dg.DataKeyField = "ContactRequestID"
dg.DataBind()
cnn.Close()
cmd.Dispose()
cnn.Dispose()
dg.Visible = True
End Sub
Private Sub dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls. DataGridPa geChangedE ventArgs) Handles dg.PageIndexChanged
Dim dt As DataTable = GetTableData()
Dim dv As DataView = New DataView(dt)
Dim dataGrid As DataGrid = source
dataGrid.CurrentPageIndex = e.NewPageIndex
dv.Sort = dataGrid.Attributes("SortE xpression" )
If dataGrid.Attributes("SortA SC") = "No" Then
dv.Sort &= " DESC"
End If
dataGrid.DataSource = dv
dataGrid.DataBind()
End Sub
Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls. DataGridCo mmandEvent Args) Handles dg.ItemCommand
'If e.CommandName = "Edit" Then
'Response.Redirect("EditBr oadBandReq uest.aspx? crid=" & CStr(dg.DataKeys(e.Item.It emIndex)))
'End If
If e.CommandName = "Assign" Then
Response.Redirect("AssignB roadBandRe quest.aspx ?crid=" & CStr(dg.DataKeys(e.Item.It emIndex)))
End If
End Sub
Private Sub SearchProducts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchProducts.Click
Dim LastName, Phone As String
Select Case FieldsToMatch.SelectedValu e
Case "LastName"
LastName = SearchQuery.Text
dg.CurrentPageIndex = 0
BindTheData(LastName, "")
FullQuery.Value = LastName
Case "PhoneNumber"
Phone = SearchQuery.Text
dg.CurrentPageIndex = 0
BindTheData("", Phone)
FullQuery.Value = Phone
End Select
End Sub
Private Sub LinkButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
Response.Redirect("default .aspx")
End Sub
Private Sub dg_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls. DataGridSo rtCommandE ventArgs) Handles dg.SortCommand
Dim strSort = dg.Attributes("SortExpress ion")
Dim strASC = dg.Attributes("SortASC")
dg.Attributes("SortExpress ion") = e.SortExpression
dg.Attributes("SortASC") = "Yes"
If e.SortExpression = strSort Then
If strASC = "Yes" Then
dg.Attributes("SortASC") = "No"
Else
dg.Attributes("SortASC") = "Yes"
End If
End If
Dim dt As DataTable = GetTableData()
Dim dv As DataView = New DataView(dt)
dv.Sort = dg.Attributes("SortExpress ion")
If dg.Attributes("SortASC") = "No" Then
dv.Sort &= " DESC"
End If
dg.CurrentPageIndex = 0
dg.DataSource = dv
dg.DataBind()
End Sub
Function GetTableData() As DataTable
Dim cnn As New SqlClient.SqlConnection(Co nfiguratio nSettings. AppSetting s("Emerald ConnStr"))
Dim cmd As New SqlClient.SqlCommand
Dim da As New SqlClient.SqlDataAdapter(c md)
Dim ds As New DataSet
da.SelectCommand = cmd
cmd.CommandType = CommandType.StoredProcedur e
cmd.CommandText = "Miscellaneous.dbo.Contact RequestVie wAll"
cmd.Parameters.Add(New SqlParameter("@LastName", ""))
cmd.Parameters.Add(New SqlParameter("@Phone", ""))
cmd.Connection = cnn
cnn.Open()
da.Fill(ds, "ContactRequests")
Return ds.Tables("ContactRequests ")
End Function
Dim authTicket As FormsAuthenticationTicket = CType(HttpContext.Current.
HttpContext.Current.User = New GenericPrincipal(User.Iden
End Sub
Private Sub BindTheData(Optional ByVal LastName As String = "", Optional ByVal Phone As String = "")
Dim cnn As New SqlClient.SqlConnection(Co
Dim cmd As New SqlClient.SqlCommand
Dim da As New SqlClient.SqlDataAdapter(c
Dim ds As New DataSet
cmd.CommandType = CommandType.StoredProcedur
cmd.CommandText = "Miscellaneous.dbo.Contact
cmd.Parameters.Add(New SqlParameter("@LastName", LastName))
cmd.Parameters.Add(New SqlParameter("@Phone", Phone))
cmd.Connection = cnn
cnn.Open()
da.SelectCommand = cmd
da.Fill(ds, "ContactRequests")
dg.DataSource = ds
dg.DataKeyField = "ContactRequestID"
dg.DataBind()
cnn.Close()
cmd.Dispose()
cnn.Dispose()
dg.Visible = True
End Sub
Private Sub dg_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
Dim dt As DataTable = GetTableData()
Dim dv As DataView = New DataView(dt)
Dim dataGrid As DataGrid = source
dataGrid.CurrentPageIndex = e.NewPageIndex
dv.Sort = dataGrid.Attributes("SortE
If dataGrid.Attributes("SortA
dv.Sort &= " DESC"
End If
dataGrid.DataSource = dv
dataGrid.DataBind()
End Sub
Private Sub dg_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
'If e.CommandName = "Edit" Then
'Response.Redirect("EditBr
'End If
If e.CommandName = "Assign" Then
Response.Redirect("AssignB
End If
End Sub
Private Sub SearchProducts_Click(ByVal
Dim LastName, Phone As String
Select Case FieldsToMatch.SelectedValu
Case "LastName"
LastName = SearchQuery.Text
dg.CurrentPageIndex = 0
BindTheData(LastName, "")
FullQuery.Value = LastName
Case "PhoneNumber"
Phone = SearchQuery.Text
dg.CurrentPageIndex = 0
BindTheData("", Phone)
FullQuery.Value = Phone
End Select
End Sub
Private Sub LinkButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
Response.Redirect("default
End Sub
Private Sub dg_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.
Dim strSort = dg.Attributes("SortExpress
Dim strASC = dg.Attributes("SortASC")
dg.Attributes("SortExpress
dg.Attributes("SortASC") = "Yes"
If e.SortExpression = strSort Then
If strASC = "Yes" Then
dg.Attributes("SortASC") = "No"
Else
dg.Attributes("SortASC") = "Yes"
End If
End If
Dim dt As DataTable = GetTableData()
Dim dv As DataView = New DataView(dt)
dv.Sort = dg.Attributes("SortExpress
If dg.Attributes("SortASC") = "No" Then
dv.Sort &= " DESC"
End If
dg.CurrentPageIndex = 0
dg.DataSource = dv
dg.DataBind()
End Sub
Function GetTableData() As DataTable
Dim cnn As New SqlClient.SqlConnection(Co
Dim cmd As New SqlClient.SqlCommand
Dim da As New SqlClient.SqlDataAdapter(c
Dim ds As New DataSet
da.SelectCommand = cmd
cmd.CommandType = CommandType.StoredProcedur
cmd.CommandText = "Miscellaneous.dbo.Contact
cmd.Parameters.Add(New SqlParameter("@LastName", ""))
cmd.Parameters.Add(New SqlParameter("@Phone", ""))
cmd.Connection = cnn
cnn.Open()
da.Fill(ds, "ContactRequests")
Return ds.Tables("ContactRequests
End Function
ASKER
>>you should just use the field in the db for the sort expression. If you bind to a dataset all the sorting logic is handled by it.
what do you mean by that
what do you mean by that
if you set the sort expression to the name of the field and use the above code you don't have to do any additional logic handling.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Aeros