我的ASP网站上有一个gridview需要处理每一行,并根据条件保留行或删除它并将其添加到另一个gridview。我已经想出了如何通过简单地使用其中一个内置方法来删除行。我在网上找到的所有东西都告诉我使用“gridview.Rows.Add(row)”属性,但它在Visual Studio中创建了以下错误:
“'添加'不是'System.Web.UI.WebControls.GridViewRowCollection'的成员。”
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
答案 0 :(得分:1)
我可以使用以下代码在网格视图中添加一行。我基本上不得不重新完成整个过程,将嵌套的gridview视为一个全新的gridview。这仅适用于您计划在嵌套网格视图中放置一行的情况。您可以使用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()
答案 1 :(得分:0)
我认为您需要在grdExpTraining上使用CopyTo,将该行添加到ArrayList,然后再次初始化GridViewRowCollection。
答案 2 :(得分:0)
如果你想在gridview上自动行,比如从db记录计数得到一些学生信息是n,那么这可能会有所帮助。
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,然后只需删除循环。