Asp.net gridview服务器端代码与集合类绑定问题 [英] Asp.net gridview serverside code with collection class to bind problem
问题描述
大家好,这里遇到了问题.如您所见,我在应用程序代码上使用集合进行绑定.如果我的代码是这样的
Hello guys, got a problem here. As you see I am binding using collection on app code. if my code is like this one
_userDAL = New UserDAL
Dim users As UserCollection = _userDAL.SelectAll()
'Dim dr As DataTable = getTable()
gvUserList.DataSource = users
gvUserList.DataBind()
一切正常,但没有分页.现在要进行分页,我需要将其绑定到数据表上,所以这是我的代码了
everything works fine but it doesnt have paging. Now to have a paging I need to bind it on datatable so this is my code now
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
bind()
End Sub
Private Sub bind()
_userDAL = New UserDAL
Dim users As UserCollection = _userDAL.SelectAll()
Dim dr As DataTable = getTable()
gvUserList.DataSource = dr
gvUserList.DataBind()
End Sub
Private Function getTable() As DataTable
_userDAL = New UserDAL
Dim users As UserCollection = _userDAL.SelectAll()
Dim adap As New SqlDataAdapter(users)
Dim dt As New DataTable()
adap.Fill(dt)
Return dt
End Function
但我收到一个错误消息,说''collection'' cannot be converted to sqlcommand
指向用户.我知道我的逻辑是错误的,但是我找不到关于如何将GridView绑定到集合上的合适教程.你能帮我吗?
UserDAL.vb
but im getting an error saying ''collection'' cannot be converted to sqlcommand
pointing at users. I know that my logic is wrong but i cant find a suitable tutorial regarding on how to bind gridview on regards to collection. Could you help me?
UserDAL.vb
Public Function SelectAll() As UserCollection
Try
'select * from User
_sqlConn = New SqlConnection(_connString)
_sqlConn.Open()
Dim sqlCmd As New SqlCommand("select * from [Users]", _sqlConn)
Dim dr As SqlDataReader = sqlCmd.ExecuteReader()
'Create user collection
Dim userColl As New UserCollection
Dim user As User
While dr.Read()
'Create User object
user = New User
user.UserName = dr("UserName").ToString
user.Password = dr("Password").ToString
user.FirstName = dr("FirstName").ToString
user.Surname = dr("Surname").ToString
'add newley created user to collection
userColl.Add(user)
End While
dr.Close()
Return userColl
Finally
If _sqlConn IsNot Nothing Then
If _sqlConn.State = Data.ConnectionState.Open Then
_sqlConn.Close()
End If
End If
End Try
End Function
UserCollection.vb
UserCollection.vb
Public Class UserCollection
Inherits CollectionBase
Public Sub Add(ByVal item As User)
List.Add(item)
End Sub
Public Sub Remove(ByVal item As User)
List.Remove(item)
End Sub
Default Public Property Item(ByVal index As Integer) As User
Get
Return DirectCast(List(index), User)
End Get
Set(ByVal value As User)
List(index) = value
End Set
End Property
推荐答案
在您的DAL中,我应该以这种方式返回
用户列表
Hi,
In your DAL I thing you should do it this way to return the
List of User
Public Shared Function SelectAll() As List(Of User)
''Create List Of User
Dim userColl As New List(Of User)
Try
''select * from User
_sqlConn = New SqlConnection(_connString)
_sqlConn.Open()
Dim sqlCmd As New SqlCommand("select * from [Users]", _sqlConn)
Dim dr As SqlDataReader = sqlCmd.ExecuteReader()
While dr.Read()
''Create User object
Dim userRec As User = New User
userRec.UserName = dr("UserName").ToString
userRec.Password = dr("Password").ToString
userRec.FirstName = dr("FirstName").ToString
userRec.Surname = dr("Surname").ToString
''add newly created userRec to List Of User
userColl.Add(userRec)
End While
dr.Close()
Catch
''add something here to catch exption message ...?
End Try
Finally
If _sqlConn IsNot Nothing Then
If _sqlConn.State = Data.ConnectionState.Open Then
_sqlConn.Close()
End If
End If
End Try
Return userColl
End Function
问候,
代数.
Regards,
Algem.
这篇关于Asp.net gridview服务器端代码与集合类绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!