Hi first I want say thank you for you help, I've been searching for days to assist me on doing my grid. I have 1 little issue. I keep getting this error msg
"Unable to cast object of type 'System.Data.DataView' to type 'System.Data.DataTable'. " on this line "GridViewTransHist.DataSource = SortDataTable(GridViewTransHist.DataSource, False)". What am I doing wrong?
my code below.
Partial Class _Default
Inherits System.Web.UI.Page
Dim sqlConn As Data.SqlClient.SqlConnection = New _
Data.SqlClient.SqlConnection("Data Source=CCBLOANDATA\DEVELOPMENT;Initial Catalog=LoanRev;Integrated Security=True")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'setup transaction type dropdownlist box
Dim cmdSqlTransType As Data.SqlClient.SqlCommand = New _
Data.SqlClient.SqlCommand("select txn_cde, (txn_cde + ': ' + isnull(trans_function,'<Unknown description>')) as trans_function " & _
"from v_alldaytxn group by txn_cde, trans_function order by txn_cde", sqlConn)
sqlConn.Open()
drpdwnTransCode.DataSource = cmdSqlTransType.ExecuteReader()
drpdwnTransCode.DataTextField = "trans_function"
drpdwnTransCode.DataValueField = "txn_cde"
'dont want the DropDownList to load again
If Not IsPostBack Then
drpdwnTransCode.DataBind()
drpdwnTransCode.Items.Insert(0, "ALL")
End If
sqlConn.Close()
End Sub
Protected Sub btnGetData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetData.Click
PopulateTransHistGrid()
End Sub
Private Sub PopulateTransHistGrid()
Dim sqlwhere As String
Dim gridquery As String
If drpdwnTransCode.Text = "ALL" Then
sqlwhere = " "
Else
sqlwhere = " txn_cde = " & drpdwnTransCode.Text & " AND "
End If
sqlwhere = sqlwhere & " txn_eff_dt <= '" & txtenddt.SelectedDateFormatted & "'" & _
" AND txn_eff_dt >= '" & txtbegdt.SelectedDateFormatted & "'"
gridquery = "SELECT Escr_amt, escr_res_amt, Int_amt, late_chg_amt, " & _
"Principal, unapplied_amt, txn_cde, is_cash_trans, acct_nbr, " & _
"cash_trans_type, trans_function, CONVERT(char(10), txn_eff_dt, 101) as txn_eff_dt, txn_amt " & _
"FROM v_alldaytxn where " & sqlwhere & " order by txn_eff_dt desc"
sqlConn.Open()
Dim dsTranHist As Data.DataSet
Dim cmdTranHist As Data.SqlClient.SqlDataAdapter
cmdTranHist = New Data.SqlClient.SqlDataAdapter(gridquery, sqlConn)
dsTranHist = New Data.DataSet()
cmdTranHist.Fill(dsTranHist, "v_alldaytxn")
Dim dataTableRowCount As Integer = GridViewTransHist.Rows.Count
'If dataTableRowCount > 0 Then
GridViewTransHist.DataSource = dsTranHist.Tables("v_alldaytxn").DefaultView
GridViewTransHist.DataBind()
sqlConn.Close()
'End If
End Sub
Protected Sub GridViewTransHist_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridViewTransHist.PageIndexChanging
GridViewTransHist.DataSource = SortDataTable(GridViewTransHist.DataSource, True)
GridViewTransHist.PageIndex = e.NewPageIndex
GridViewTransHist.DataBind()
End Sub
Private Property GridViewSortDirection() As String
Get
Return IIf(ViewState("SortDirection") = Nothing, "ASC", ViewState("SortDirection"))
End Get
Set(ByVal value As String)
ViewState("SortDirection") = value
End Set
End Property
Private Property GridViewSortBLOCKED EXPRESSION As String
Get
Return IIf(ViewState("SortExpression") = Nothing, String.Empty, ViewState("SortExpression"))
End Get
Set(ByVal value As String)
ViewState("SortExpression") = value
End Set
End Property
Private Function GetSortDirection() As String
Select Case GridViewSortDirection
Case "ASC"
GridViewSortDirection = "DESC"
Case "DESC"
GridViewSortDirection = "ASC"
End Select
Return GridViewSortDirection
End Function
Protected Function SortDataTable(ByVal dataTable As Data.DataTable, ByVal isPageIndexChanging As Boolean) As Data.DataView
If Not dataTable Is Nothing Then
Dim dataView As New Data.DataView(dataTable)
If GridViewSortExpression <> String.Empty Then
If isPageIndexChanging Then
dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection)
Else
dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetSortDirection())
End If
End If
Return dataView
Else
Return New Data.DataView()
End If
End Function
Protected Sub GridViewTransHist_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridViewTransHist.Sorting
GridViewSortExpression = e.SortExpression
Dim pageIndex As Integer = GridViewTransHist.PageIndex
GridViewTransHist.DataSource = SortDataTable(GridViewTransHist.DataSource, False)
GridViewTransHist.DataBind()
GridViewTransHist.PageIndex = pageIndex
End Sub
End Class