从VB.NET动态填充后ASP.NET GridView清空

时间:2013-07-30 17:37:02

标签: sql vb.net dataset oledbcommand oledbdataadapter

我有一个ASP.NET GridView没有填充VB.NET Fill中的新行。我正在使用的代码的最新版本如下:

Protected Sub uGV(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click 
    Dim rFm As DateTime = DateTime.Parse(calFrom.SelectedDate.Date.ToString)
    Dim rTo As DateTime = DateTime.Parse(calTo.SelectedDate.Date.ToString)
    Dim oleUpConnString As String = ConfigurationManager.ConnectionStrings("OLEDBConnectionStringtoSQL").ConnectionString
    Dim oleUpConn As New OleDbConnection()
    oleUpConn.ConnectionString = oleUpConnString
    oleUpConn.Open()
    Dim vUp As New OleDbDataAdapter()
    Dim upCmd As String = "SELECT * FROM mail WHERE (date BETWEEN '07/02/2013' AND '07/04/2013')"
    Dim dsUp As New DataSet()
    Dim dt As New DataTable("Table1")
    Dim upCmdEx As New OleDbCommand(upCmd, oleUpConn)
    With upCmdEx.Parameters
        .AddWithValue("?", rFm)
        .AddWithValue("?", rTo)
    End With
    Try
        vUp.SelectCommand = upCmdEx
    Catch ex As Exception
        MsgBox("The select command failed")
    End Try
    Try
        Me.GridView2.DataSource = dsUp.Tables(0)
        vUp.Fill(dsUp)
    Catch ex As Exception
        MsgBox("Filling the dataset failed")
    End Try
    Me.GridView2.DataSource = dsUp
    'Me.GridView2.DataBind()
    oleUpConn.Close()
End Sub

我花了几个小时没有决议,所以如果有人能够确定我错过了什么,我将不胜感激。

编辑 - GridView编码如下:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="True" 
                    BackColor="White"  CaptionAlign="Top" 
                    CellPadding="4" ForeColor="#333333" 
                    GridLines="None" Caption="Surveys Completed">
                    <RowStyle BackColor="#EFF3FB" />
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <EditRowStyle BackColor="#2461BF" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>

2 个答案:

答案 0 :(得分:0)

如果没有看到您的代码,那么我猜您要么未将AutoGenerateColumns的{​​{1}}属性设置为“True”,要么未在结构中定义GridView您的Columns

要让GridView自动显示数据源中的所有列(以及名称),请执行以下操作:

GridView

要更好地控制要显示的列和/或列具有的标题名称,请执行以下操作:

<asp:GridView id="GridView2" runat="server" AutoGenerateColumns="True" />

注意:显然,在此示例中组成了<asp:GridView id="GridView2" runat="server"> <Columns> <asp:boundfield datafield="CustomerID" headertext="Customer ID" /> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="Address" headertext="Address"/> <asp:boundfield datafield="City" headertext="City"/> <asp:boundfield datafield="PostalCode" headertext="Postal Code"/> <asp:boundfield datafield="Country" headertext="Country"/> </Columns> </asp:GridView> ,您可以将这些名称与数据字段相匹配。

答案 1 :(得分:0)

在花费了大量时间之后,这里是GridView动态视图功能背后代码的最终版本,它将实时更新,无需刷新页面。

Protected Sub uGV(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click 
    Dim rFm As String = String.Empty
    Dim rTo As String = String.Empty
    rFm = calFrom.SelectedDate.Date.ToString
    rTo = calTo.SelectedDate.Date.ToString
    Dim oleUpConn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("OLEDBConnectionStringtoSQL").ConnectionString)
    Dim vUp As OleDbDataAdapter = New OleDbDataAdapter
    Dim selectSQL As String = "SELECT * FROM mail WHERE (date BETWEEN ? AND ?)"
    Dim upCmdEx As OleDbCommand = New OleDbCommand(selectSQL, oleUpConn)
    vUp.SelectCommand = upCmdEx
    upCmdEx.Parameters.AddWithValue("@FROMDATE", rFm)
    upCmdEx.Parameters.AddWithValue("@TODATE", rTo)
    Dim dsUp As DataSet = New DataSet
    vUp.Fill(dsUp, "DynamicView")
    If dsUp.Tables("DynamicView").Rows.Count > 0 Then
        GridView2.DataSource = dsUp.Tables("DynamicView")
        GridView2.DataBind()
    Else
        MsgBox("Nothing to display for the selected date range.  Please select a new date range and try again.")
        Exit Sub
    End If
End Sub