0

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

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>
4

2 回答 2

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
于 2013-07-31T19:01:48.467 回答
0

没有看到你的代码,那么我猜你要么没有将AutoGenerateColumns的属性设置GridView为“True”,要么你没有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>

注意:显然boundfields 是在这个例子中组成的,您可以将名称与您的数据字段匹配。

于 2013-07-30T18:00:55.437 回答