简体   繁体   English

如何动态地将指定数量的文本框添加到gridview?

[英]How do I dynamically add specified number of textboxes to gridview?

The code below allows users to add as many rows of textboxes to the gridview as needed. 下面的代码允许用户根据需要向gridview添加尽可能多的文本框行。

We have decided to limit the number of rows a user adds dynamically to just 6. 我们决定将用户动态添加的行数限制为6。

I have googled the web looking for samples of how this is done to no avail. 我在网上搜寻了一些样本,以寻找如何完成此事的样本。

Does anyone know what needs to change on the code below so that users can only add new rows of up to 6 but no more than 6? 有谁知道需要对以下代码进行哪些更改,以便用户只能添加最多6个但不超过6个的新行?

Thanks a lot in advance 提前谢谢

    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

Note: I added C# tag since I can convert from C# to VB.net 注意:我添加了C#标记,因为我可以从C#转换为VB.net

You should have a counter for the rows. 您应该对行有一个计数器。 When a user adds a row the counter will increase. 当用户添加一行时,计数器将增加。 If a user tries to add a row when counter >= limit do not allow it. 如果用户尝试在counter >= limit时添加行,则不允许这样做。

You should have an UserAddRow and UserRemoveRow methods which will take care of the counter increase and decrese. 您应该有一个UserAddRowUserRemoveRow方法,这些方法将处理计数器的增加和减少。 This will improve readability and maintainability . 这将提高可读性可维护性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM