您好我正在设计Web应用程序,因为当我单击“搜索”按钮时,已存储的数据应显示为网格视图。现在我已经使用asp.net创建了网格视图,并在View代码中为网格视图创建了代码。我没有任何错误,但是当我点击搜索时,我没有显示任何数据。以下是视图代码。
Private Sub BindGridView()
Dim filter As String = ""
Dim table1 As DataTable = New DataTable("Details")
table1.Columns.Add("ReferenceNo")
table1.Columns.Add("Title")
table1.Columns.Add("CounterParty")
table1.Columns.Add("OwnerDepartment")
table1.Columns.Add("Status")
table1.Columns.Add("CreatedBy")
table1.Columns.Add("CreatedOn")
table1.Rows.Clear()
Dim conSQL As New SqlConnection(conStrDRS)
conSQL.Open()
Dim cmdSQL As SqlCommand = New SqlCommand("SELECT ReferenceNo, Title, CounterParty, OwnerDepartment, Status, CreatedBy, CreatedOn FROM dbo.Registration")
cmdSQL.Connection = conSQL
Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL)
Dim myDataSet As New DataSet()
adptSQL.Fill(myDataSet)
conSQL.Close()
Dim set1 As DataSet = New DataSet("Details")
set1.Tables.Add(table1)
Dim dvDetails As DataView = table1.DefaultView
gvDetails.DataSource = table1
gvDetails.DataBind()
End Sub
以下是“搜索”按钮的代码
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
BindGridView()
End Sub
任何人都可以帮我解决这个问题。感谢
答案 0 :(得分:0)
问题是您没有将返回的数据分配给网格。 无需创建表。返回的数据集包含适合您的表格。
在Page_Load
Private Sub BindGridView()
Dim filter As String = ""
Dim conSQL As New SqlConnection(conStrDRS)
conSQL.Open()
Dim cmdSQL As SqlCommand = New SqlCommand("SELECT ReferenceNo, Title, CounterParty, OwnerDepartment, Status, CreatedBy, CreatedOn FROM dbo.Registration")
cmdSQL.Connection = conSQL
Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL)
Dim myDataSet As New DataSet()
adptSQL.Fill(myDataSet)
conSQL.Close()
gvDetails.DataSource = myDataSet.Tables[0]
gvDetails.DataBind()
End Sub
将此称为搜索
Private Sub BindGridView(ByVal searchTerm As String)
Dim filter As String = searchTerm
Dim conSQL As New SqlConnection(conStrDRS)
conSQL.Open()
Dim cmdSQL As SqlCommand = New SqlCommand("SELECT ReferenceNo, Title, CounterParty, OwnerDepartment, Status, CreatedBy, CreatedOn FROM dbo.Registration WHERE ReferenceNo =" + searchTerm)//Add any term you want
cmdSQL.Connection = conSQL
Dim adptSQL As New SqlClient.SqlDataAdapter(cmdSQL)
Dim myDataSet As New DataSet()
adptSQL.Fill(myDataSet)
conSQL.Close()
gvDetails.DataSource = myDataSet.Tables[0]
gvDetails.DataBind()
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
BindGridView(txtSearchTerm.Text)
End Sub
答案 1 :(得分:0)
您正在填充myDataSet
,但将gridview
与新DataTable
绑定为空,因此您需要将其与myDataSet
绑定。
gvDetails.DataSource = myDataSet.Tables[0];
gvDetails.DataBind()
您不需要致电
Open
的{{1}}和Close
方法 使用SqlConnection
时。
您正在创建额外的SqlDataAdapter
,因为您没有在任何地方使用这些内容。