在Visual Basic中将行添加到gridview [英] Add row to gridview in Visual Basic
问题描述
我在我的ASP网站上有一个gridview需要处理每一行,并根据条件保留行或将其删除并将其添加到另一个gridview。我已经想通过简单地使用其中一种内置方法来移除行。我在网上找到的所有东西都告诉我使用gridview.Rows.Add(row)属性,但它会在Visual Studio中创建以下错误:
'Add '不是'System.Web.UI.WebControls.GridViewRowCollection'的成员。
- grdTraining是包含要检查的结果的主gridview。
- grdExpTraining是secondarygridview,它接受从master中取出的行。
-
grdTraining_RowDataBound是一个被调用的方法每次网站发现记录放入grdTraining。
Protected Sub grdTraining_RowDataBound(ByVal sender As Object,ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)处理grdTraining.RowDataBound
'灰过期培训课程
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
尝试
Dim expDate As Date = incomingDate
If(expDate< DateTime.Today)Then
grdExpTraining.Rows.Add(row)'导致错误的行
grdTraining.DeleteRow(trnIndex)
结束如果
赶上例外
结束尝试
trnIndex + = 1
结束Sub
一行到我的网格视图使用下面的代码。我基本上必须重新执行整个过程,将嵌套的gridview视为一个全新的gridview。这也只适用于你计划在嵌套的gridview中放置一行。您可以使用DataTable和DataRow声明的范围来进行其他操作。
'创建数据表和列
Dim dtable As New DataTable
dtable.Columns.Add(New DataColumn (StateCode))
dtable.Columns.Add(新DataColumn(CourseDesc))
dtable.Columns.Add(新DataColumn(Hours))
dtable.Columns。添加新DataColumn(EffectiveDate))
dtable.Columns.Add(新DataColumn(ExpirationDate))
dtable.Columns.Add(新DataColumn(LastChange))
创建计数器以防止出现异常
Dim i As Integer = row.Cells.Count
'为RowValues创建对象
Dim RowValues As Object() = {,,,,,}
'适当填充行值
对于索引As整数= 0至i - 1
RowValues(index)= row.Cells(index).Text
Next
'创建新数据r ow
Dim dRow As DataRow
dRow = dtable.Rows.Add(RowValues)
dtable.AcceptChanges()
'现在将datatable绑定到gridview ...
grdExpTraining.DataSource = dtable
grdExpTraining.DataBind()
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:
"'Add' is not a member of 'System.Web.UI.WebControls.GridViewRowCollection'."
- grdTraining is the "master" gridview containing the results to be examined.
- grdExpTraining is the "secondary" gridview that takes the rows pulled from the "master."
grdTraining_RowDataBound is a method that gets called everytime the website finds records to place into 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
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.
'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()
这篇关于在Visual Basic中将行添加到gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!