0

我有一个Gridview由 a 填充的dataTable,由 a 填充的DataAdapter。这就是网格最初加载的方式Page_Load。为了添加搜索功能,我做同样的事情,但TextBox.Text作为参数传递给SELECT... LIKE ...语句。要添加编辑功能(每行中的一个按钮),我dataTable需要 dataTable. 问题是,我不知道如何保持其值有效(持久),并且dataTable当我按下编辑按钮时有 0 列,因此它不显示任何要编辑的内容。我想这是因为我正在使用Using,并且dataTable可能会在之后被清理End Using

在这种情况下,我能做些什么来解决它?我想删除miconn.Close()但它没有解决任何问题,事实上,我不知道连接是否仍然打开End Using

代码:

Dim con As New Connection
Dim table As New DataTable()

Private Sub fill_grid()

    Using miconn As New SqlConnection(con.GetConnectionString())
        Dim sql As String = "SELECT area,lider_usuario FROM AREA"
        Using command As New SqlCommand(sql, miconn)
            Using ad As New SqlDataAdapter(command)
                ad.Fill(table)
                GridView1.DataSource = table
                GridView1.DataBind()
                'miconn.Close()
            End Using
        End Using
    End Using

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
        fill_grid()
        End If

End Sub

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

        Dim miCon As New Connection
        Using miconn As New SqlConnection(miCon.GetConnectionString())
            Dim sql As String = "SELECT area,lider_usuario FROM AREA WHERE area LIKE @area"
            Using command As New SqlCommand(sql, miconn)
                command.Parameters.Clear()
                command.Parameters.Add("@area", SqlDbType.VarChar).Value = "%" + TextBox1.Text + "%"
                Using ad As New SqlDataAdapter(command)
                    ad.Fill(table)
                    GridView1.DataSource = table
                    GridView1.DataBind()
                    'miconn.Close()
                End Using
            End Using
        End Using
End Sub

  Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
        GridView1.EditIndex = e.NewEditIndex
        GridView1.DataSource = table
        GridView1.DataBind()
    End Sub

   Protected Sub CancelEditRow(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
        GridView1.EditIndex = -1
        GridView1.DataSource = table
        GridView1.DataBind()
    End Sub
4

2 回答 2

1
BindGrid()
{
   var dt = YourMethodReturningDatatable();
   ViewState["Table"] = dt;
   grid.DataSource = ViewState["Table"];
   grid.Databind();
}

page_load
{
   if(not ispostback) // not because my 1 key stopped working.
   {
     BindGrid();
   }
}
于 2013-09-12T19:20:59.003 回答
0

我建议您创建两个不同的函数来填充数据表并绑定它。在 !IsPostBack 条件之前调用填充函数并在条件内进行绑定。这是示例代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim table as new DataTable
        table = GetData() 'The function that would return a data table
        If Not IsPostBack Then
           GridView1.DataSource = table
           GridView1.DataBind()
        End If

End Sub

Private Function GetData() As DataTable
Using miconn As New SqlConnection(con.GetConnectionString())
    Dim sql As String = "SELECT area,lider_usuario FROM AREA"
    Using command As New SqlCommand(sql, miconn)
        Using ad As New SqlDataAdapter(command)
            ad.Fill(table)
            GetData = table
            miconn.Close()
        End Using
    End Using
End Using
End function

希望能帮助到你。

于 2013-09-12T19:17:43.047 回答