我在解决GridView的DataSource问题时遇到了问题。我尝试过编码的多种变体,每种变体都有自己的问题。我使用GridView配置管理器定义了DataSourceID。当我尝试调用我的存储过程来填充我的GridView时,我得不到任何结果。因此,我尝试在代码隐藏中定义一个DataSource并手动绑定(并设置DataSourceID =''以避免错误)。这很好地填充了我的GridView,但不允许排序。
这是我的问题:有没有人看到我做错了什么,这些解决方案都不起作用?我从另一个项目中重新调整了代码,它们是相同的(在其他项目中,使用DataSourceID和Datasource都没有错误?)这是我的ASP.net Gridview和Proc代码:
<asp:GridView ID="gvUserSearch" runat="server" style="z-index: 1; left: 56px; top: 382px; position: absolute; height: 133px; width: 187px" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="UserSearchDataSource" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:CommandField ShowSelectButton="True" >
<ItemStyle ForeColor="#0066FF" />
</asp:CommandField>
<asp:BoundField DataField="SubscriberID" HeaderText="SubscriberID" SortExpression="SubscriberID" Visible="False" />
<asp:BoundField DataField="Last Name" HeaderText="Last Name" SortExpression="Last Name" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="First Name" HeaderText="First Name" SortExpression="First Name" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="EmailAddress" HeaderText="Email Address" SortExpression="EmailAddress" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="locationid" HeaderText="Unit ID" SortExpression="locationid" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="unit_name" HeaderText="Unit Name" SortExpression="unit_name" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Birthday" HeaderText="Birthday" ReadOnly="True" SortExpression="Birthday" DataFormatString="{0:MMMM/DD/yyyy}" HtmlEncode="False" HtmlEncodeFormatString="False" >
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Zip Code" HeaderText="Zip Code" SortExpression="Zip Code" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Status" HeaderText="Status" ReadOnly="True" SortExpression="Status" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="DateUnsubscribed" HeaderText="Unsubscribe Date" ReadOnly="True" SortExpression="DateUnsubscribed" DataFormatString="{0:MMMM/DD/yyyy}" HtmlEncode="False" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:BoundField>
</Columns>
<asp:SqlDataSource ID="UserSearchDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:CafeWorksConnectionString %>" SelectCommand="MarketingPortal_UserSearchProc" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="txtEmail" Name="Email" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="txtLastName" Name="LastName" PropertyName="Text" Type="String" />
<asp:ControlParameter ControlID="txtFirstName" Name="FirstName" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
这是我的VB.net代码隐藏:
Dim CafeWorksConnection As New SqlConnection()
Dim CafeWorksCS As String
Dim CafeWorksDA As SqlDataAdapter
Dim CafeWorksCB As SqlCommandBuilder
Dim CafeWorksDS As New DataSet
Dim CafeWorksSqlC As New SqlCommand
Dim CafeWorksReader As SqlDataReader
Private Sub DBConnect()
'Get the connection string from the web.config file
CafeWorksCS = ConfigurationManager.ConnectionStrings("CafeWorksConnectionString").ConnectionString
'Assign the Connection String to the Connection
CafeWorksConnection.ConnectionString = CafeWorksCS
'Open the database connection
CafeWorksConnection.Open()
End Sub
Private Sub Populate_GridView()
'Make a database connection
DBConnect()
'Define the type of query being executed (Stored Procedure)
CafeWorksSqlC.CommandType = CommandType.StoredProcedure
CafeWorksSqlC.CommandText = "MarketingPortal_UserSearchProc "
'Define the stored procedure parameters
CafeWorksSqlC.Parameters.AddWithValue("@Email", txtEmail.Text)
CafeWorksSqlC.Parameters.AddWithValue("@LastName", txtLastName.Text)
CafeWorksSqlC.Parameters.AddWithValue("@FirstName", txtFirstName.Text)
'Make a connection for the stored procedure to run
CafeWorksSqlC.Connection = CafeWorksConnection
'CafeWorksConnection.Open()
'Executes the stored procedure and stores the result set
CafeWorksReader = CafeWorksSqlC.ExecuteReader()
'You need to bind the data to the GridView
'Got error that DataSourceID and DataSource can't be defined (DataSourceID define in Gridview ASP.net
'code and is not giving me a result set for some reason, so I added the DataSource and DataBind
'gvUserSearch.DataSourceID = ""
gvUserSearch.DataSource = CafeWorksDS
gvUserSearch.DataBind()
'Always close the database connection when you are finished
CafeWorksConnection.Close()
End Sub
Protected Sub btnNameSearch_Click(sender As Object, e As EventArgs) Handles btnNameSearch.Click
'Call the Sub Populate_GridView to display results of search
Populate_GridView()
'Clear out the text boxes - Keeps data from lingering into other searches
txtEmail.Text = ""
txtLastName.Text = ""
txtFirstName.Text = ""
End Sub
我还是比较新的,我的代码可能不太漂亮。我很感激任何人都能给我的帮助。谢谢!
答案 0 :(得分:1)
您的首要问题是您没有定义onsorting
的{{1}}属性,如下所示:
GridView
这表示每当需要排序操作时的事件处理程序,例如用户单击列标题以对其进行排序。
我建议您将onsorting="gvUserSearch_Sorting"
类型更改为DataSource
,因为DataTable
数据结构有助于构建针对它的视图,稍后您将看到我们如何才能轻松应用列的排序方向和排序表达式。这是一个实用程序函数,可以返回DataTable
属性使用的DataTable
:
DataSource
注意:您需要更改Private Function GetGridViewDataSource() As DataTable
Dim dtGrid As New DataTable()
Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
' Change this to either execute the SQL you want or change the command object to execute a stored procedure
Dim strSelect As String = "SELECT FirstName,LastName,Location FROM Details"
Dim cmd As New SqlCommand(strSelect, con)
Dim dAdapter As New SqlDataAdapter(cmd)
dAdapter.Fill(dtGrid)
Return dtGrid
End Function
作业才能调用此功能,如下所示:DataSource
现在我们将源作为gvUserSearch.DataSource = GetGridViewDataSource()
,我们可以开始管理排序,特别是通过类属性跟踪排序方向(升序或降序),如下所示:
DataTable
现在我们终于准备好实现实际的排序处理程序,如下所示:
Public Property dir() As SortDirection
Get
If ViewState("dirState") Is Nothing Then
ViewState("dirState") = SortDirection.Ascending
End If
Return DirectCast(ViewState("dirState"), SortDirection)
End Get
Set
ViewState("dirState") = value
End Set
End Property
注意:此方法的作用是询问Protected Sub gvDetails_Sorting(sender As Object, e As GridViewSortEventArgs)
Dim sortingDirection As String = String.Empty
If dir = SortDirection.Ascending Then
dir = SortDirection.Descending
sortingDirection = "Desc"
Else
dir = SortDirection.Ascending
sortingDirection = "Asc"
End If
Dim sortedView As New DataView(GetGridViewDataSource())
sortedView.Sort = Convert.ToString(e.SortExpression) & " " & sortingDirection
gvDetails.DataSource = sortedView
gvDetails.DataBind()
End Sub
排序方向是什么,然后从ViewState
创建DataView
并应用在网格视图中定义的排序表达式列,以及将数据源重新绑定到网格的排序方向。