使用asp.net

时间:2015-12-14 04:27:54

标签: vb.net gridview

您好我正在设计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

任何人都可以帮我解决这个问题。感谢

2 个答案:

答案 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,因为您没有在任何地方使用这些内容。