I have a gridview on my ASP website that needs to process each row and, based on a condition, either keep the row or remove it and add it to another gridview. I've figured out how to remove the row by simply using one of the built-in methods. Everything I find online is telling me to use the "gridview.Rows.Add(row)" property, however it creates the following error in Visual Studio:
我在ASP网站上有一个gridview,它需要处理每一行,并且基于一个条件,要么保留行,要么删除它,并将它添加到另一个gridview中。我已经知道如何通过使用一个内置的方法来删除行。我在网上找到的所有东西都告诉我要使用“gridview.Rows.Add(row)”属性,但是它在Visual Studio中创建了以下错误:
"'Add' is not a member of 'System.Web.UI.WebControls.GridViewRowCollection'."
“‘Add’不是‘System.Web.UI.WebControls.GridViewRowCollection’的成员。”
- grdTraining is the "master" gridview containing the results to be examined.
- grdTraining是包含要检查的结果的“master”gridview。
- grdExpTraining is the "secondary" gridview that takes the rows pulled from the "master."
- grdExpTraining是“次要”gridview,它从“master”中提取行。
-
grdTraining_RowDataBound is a method that gets called everytime the website finds records to place into grdTraining.
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
3 个解决方案
#1
1
I was able to add a row to my grid view using the following code. I basically had to re-do the entire process again, treating the nested gridview like a completely new gridview. This also only works if you plan on placing one row in the nested gridview. You can play with the scope of the DataTable and DataRow declarations to do otherwise.
我可以使用以下代码向网格视图添加一行。基本上,我必须重新执行整个过程,将嵌套的gridview视为一个全新的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()
#2
0
I think you need to use CopyTo on the grdExpTraining, add the row to the ArrayList, and then initialize the GridViewRowCollection again.
我认为您需要在grdExpTraining上使用CopyTo,将该行添加到ArrayList中,然后再次初始化GridViewRowCollection。
#3
0
if u like to auto rows on gridview like get some students info from db record count is n, then this might be help.
如果你喜欢在gridview上自动行,比如从db记录计数中获取一些学生信息,那么这可能会有帮助。
HTML
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 CODE
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
Where you can edit the marks and remarks and INSERT INTO DB if you want to insert single row one after another then just remove the loops.
你可以编辑标记和注释,然后插入到DB中,如果你想一个接一个地插入一行,那么只需要删除循环。
#1
1
I was able to add a row to my grid view using the following code. I basically had to re-do the entire process again, treating the nested gridview like a completely new gridview. This also only works if you plan on placing one row in the nested gridview. You can play with the scope of the DataTable and DataRow declarations to do otherwise.
我可以使用以下代码向网格视图添加一行。基本上,我必须重新执行整个过程,将嵌套的gridview视为一个全新的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()
#2
0
I think you need to use CopyTo on the grdExpTraining, add the row to the ArrayList, and then initialize the GridViewRowCollection again.
我认为您需要在grdExpTraining上使用CopyTo,将该行添加到ArrayList中,然后再次初始化GridViewRowCollection。
#3
0
if u like to auto rows on gridview like get some students info from db record count is n, then this might be help.
如果你喜欢在gridview上自动行,比如从db记录计数中获取一些学生信息,那么这可能会有帮助。
HTML
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 CODE
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
Where you can edit the marks and remarks and INSERT INTO DB if you want to insert single row one after another then just remove the loops.
你可以编辑标记和注释,然后插入到DB中,如果你想一个接一个地插入一行,那么只需要删除循环。