以下代码允许用户根据需要向gridview添加任意数量的文本框行。
我们决定将用户动态添加的行数限制为仅为6。
我已经在网上搜索了如何完成此操作的样本无效。
有没有人知道下面的代码需要更改哪些内容,以便用户只能添加最多6行但不超过6行的新行?
提前多多感谢
Private Sub AddNewRow()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim TextRefillNumber As TextBox = DirectCast(grvStudentDetails.Rows(rowIndex).Cells(1).FindControl("txtRefillNumber"), TextBox)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("RowNumber") = i + 1
dtCurrentTable.Rows(i - 1)("Col1") = TextRefillNumber.Text
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
grvStudentDetails.DataSource = dtCurrentTable
grvStudentDetails.DataBind()
Dim txn As TextBox = DirectCast(grvStudentDetails.Rows(rowIndex).Cells(1).FindControl("txtRefillNumber"), TextBox)
' txn.Focus;
txn.Focus()
End If
Else
Response.Write("ViewState is null")
End If
SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
For i As Integer = 0 To dt.Rows.Count - 1
Dim TextRefillNumber As TextBox = DirectCast(grvStudentDetails.Rows(rowIndex).Cells(1).FindControl("txtRefillNumber"), TextBox)
' drCurrentRow["RowNumber"] = i + 1;
grvStudentDetails.Rows(i).Cells(0).Text = Convert.ToString(i + 1)
TextRefillNumber.Text = dt.Rows(i)("Col1").ToString()
rowIndex += 1
Next
End If
End If
End Sub
Protected Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
AddNewRow()
End Sub
注意:我添加了C#标签,因为我可以从C#转换为VB.net
答案 0 :(得分:1)
你应该有一个行的计数器。
当用户添加一行时,计数器将增加。
如果用户在counter >= limit
不允许时尝试添加行。
您应该拥有UserAddRow
和UserRemoveRow
方法
照顾柜台的增加和减少。这会改善readability和maintainability。