0

我的 ASP 网站上有一个 gridview,需要处理每一行,并根据条件保留该行或将其删除并将其添加到另一个 gridview。我已经弄清楚了如何通过简单地使用一种内置方法来删除该行。我在网上找到的所有内容都告诉我使用“gridview.Rows.Add(row)”属性,但是它在 Visual Studio 中创建了以下错误:

“‘添加’不是‘System.Web.UI.WebControls.GridViewRowCollection’的成员。”

  • grdTraining 是包含要检查的结果的“主”网格视图。
  • grdExpTraining 是“辅助”网格视图,它采用从“主”中提取的行。
  • grdTraining_RowDataBound 是一种方法,每次网站找到要放入 grdTraining 的记录时都会调用该方法。

    Protected Sub grdTraining_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdTraining.RowDataBound
    
       ' Grey out expired training courses
       Dim row As GridViewRow
       row = e.Row
       Dim incomingDate As String
       incomingDate = row.Cells(4).Text.ToString()
    
       If (e.Row.RowType <> DataControlRowType.DataRow) Then
           Exit Sub
       End If
    
       Try
           Dim expDate As Date = incomingDate
           If (expDate < DateTime.Today) Then
               grdExpTraining.Rows.Add(row)    'The line that is causing the error
               grdTraining.DeleteRow(trnIndex)
           End If
       Catch ex As Exception
       End Try
    
       trnIndex += 1
    End Sub
    
4

3 回答 3

2

我能够使用以下代码在我的网格视图中添加一行。我基本上不得不再次重新执行整个过程,将嵌套的网格视图视为全新的网格视图。这也仅在您计划在嵌套网格视图中放置一行时才有效。您可以使用 DataTable 和 DataRow 声明的范围来做其他事情。

            'Create datatable and columns
            Dim dtable As New DataTable
            dtable.Columns.Add(New DataColumn("StateCode"))
            dtable.Columns.Add(New DataColumn("CourseDesc"))
            dtable.Columns.Add(New DataColumn("Hours"))
            dtable.Columns.Add(New DataColumn("EffectiveDate"))
            dtable.Columns.Add(New DataColumn("ExpirationDate"))
            dtable.Columns.Add(New DataColumn("LastChange"))

            'Create counter to prevent out of bounds exception
            Dim i As Integer = row.Cells.Count

            'Create object for RowValues
            Dim RowValues As Object() = {"", "", "", "", "", ""}

            'Fill row values appropriately
            For index As Integer = 0 To i - 1
                RowValues(index) = row.Cells(index).Text
            Next

            'create new data row
            Dim dRow As DataRow
            dRow = dtable.Rows.Add(RowValues)
            dtable.AcceptChanges()

            'now bind datatable to gridview... 
            grdExpTraining.DataSource = dtable
            grdExpTraining.DataBind()
于 2012-09-06T22:29:04.587 回答
0

如果您喜欢在 gridview 上自动行,例如从数据库记录数中获取一些学生信息,那么这可能会有所帮助。

HTML

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    CellPadding="2" CellSpacing="1" DataKeyNames="Roll_No,ID_Number,Marks,Remarks" ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
          <asp:TemplateField HeaderText="Roll No">
            <ItemTemplate>
              <asp:Label ID="txtRoll_No" runat="server"></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="ID Number">
            <ItemTemplate>
              <asp:Label ID="txtID_Number" runat="server"></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Marks">
            <ItemTemplate>
              <asp:TextBox ID="txtMarks" runat="server" Width="50px" CssClass="text" 
            Text=""></asp:TextBox>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Remarks">
            <ItemTemplate>
              <asp:TextBox ID="txtRemarks" runat="server" CssClass="text" Width="200"></asp:TextBox>
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
      </asp:GridView>

VB.NET 代码

Dim dt As New DataTable()
Dim i As Integer
dt.Columns.Add(New DataColumn("Roll_No"))
dt.Columns.Add(New DataColumn("ID_Number"))
dt.Columns.Add(New DataColumn("Marks"))
dt.Columns.Add(New DataColumn("Remarks"))
For i = 1 To 10
  Dim dRow As DataRow      
  dRow = dt.NewRow
  dt.Rows.Add(dRow)    
  dt.AcceptChanges()      
Next i
'now bind datatable to gridview... 
GridView1.DataSource = dt
GridView1.DataBind()

With GridView1
  For i = 0 To .Rows.Count - 1
    Dim Roll_No As Label = DirectCast(.Rows(i).Cells(1).FindControl("txtRoll_No"), Label)
    Dim ID_Number As Label = DirectCast(.Rows(i).Cells(2).FindControl("txtID_Number"), Label)        
    Roll_No.Text = i + 1
    ID_Number.Text = "2014-01-" & i + 1
  Next
End With

如果要逐行插入单行,则可以在其中编辑标记和备注以及 INSERT INTO DB,然后删除循环。

于 2014-02-22T05:00:47.997 回答
0

我认为您需要在 grdExpTraining 上使用 CopyTo,将行添加到 ArrayList,然后再次初始化 GridViewRowCollection。

于 2012-09-06T14:56:12.933 回答