将数据从文本框添加到gridview

时间:2014-02-11 05:36:26

标签: excel-vba gridview textbox vba excel

此代码用于将文本框中的数据添加到网格视图,并显示与之前添加的数据相同的内容

 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try
            If Not Page.IsPostBack Then

                Me.SetInitialRow()
                'Session("grdStudents") = grdStudents
                Me.mwBooking.Visible = True
                Me.mwBooking.SetActiveView(vwForm)
                lbls.Visible = False
                btnRegister.Enabled = True
                btnRegister.Visible = True

            End If
        Catch ex As Exception
            ExceptionManager.Publish(ex)
        End Try
    End Sub
Private Sub SetInitialRow() 'method to show the initial row in grid 
        Dim dt As New DataTable
        Dim dr As DataRow = Nothing
        dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
        dt.Columns.Add(New DataColumn("Fname", GetType(String)))
        dt.Columns.Add(New DataColumn("Lname", GetType(String)))
        dt.Columns.Add(New DataColumn("Email", GetType(String)))
        dt.Columns.Add(New DataColumn("Phone", GetType(String)))
        ' dt.Columns.Add(New DataColumn("Company", GetType(String)))
        dr = dt.NewRow()
        dr("RowNumber") = 1
        dr("Fname") = String.Empty
        dr("Lname") = String.Empty
        dr("Email") = String.Empty
        dr("Phone") = String.Empty
        '   dr("Company") = "67t"
        dt.Rows.Add(dr)
        'dr = dt.NewRow();
        'Store the DataTable in ViewState
        ViewState("CurrentTable") = dt
        grdStudents.DataSource = dt
        grdStudents.DataBind()
        ' Dim TXTf As TextBox = TryCast(Me.grdStudents.Rows(0).Cells(1).FindControl("txtFnam"), TextBox)

        '  TXTf.Focus()

        '  Dim btn As Button = TryCast(grdStudents.FooterRow.FindControl("btnAdd"), Button)
        '  Page.Form.DefaultFocus = btn.ClientID
    End Sub
Private Sub AddNewRowToGrid() 'Adding new row to the grid 
       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
                    'extract the TextBox values
                    Dim lblSFname As Label = DirectCast(grdStudents.Rows(rowIndex).Cells(1).FindControl("Sfname"), Label)
                    Dim lblSLname As Label = DirectCast(grdStudents.Rows(rowIndex).Cells(2).FindControl("SLname"), Label)
                    Dim lblSEmail As Label = DirectCast(grdStudents.Rows(rowIndex).Cells(3).FindControl("SEmail"), Label)
                    Dim lblSPhone As Label = DirectCast(grdStudents.Rows(rowIndex).Cells(4).FindControl("SPhone"), Label)
                    ' Dim boxCompany As TextBox = DirectCast(grdStudents.Rows(rowIndex).Cells(4).FindControl("txtCompany"), TextBox)
                    drCurrentRow = dtCurrentTable.NewRow()
                    'save the data back to datatable
                    drCurrentRow("RowNumber") = i + 1
                    dtCurrentTable.Rows(i - 1)("Fname") = txtSFn.Text
                    dtCurrentTable.Rows(i - 1)("Lname") = txtSLN.Text
                    dtCurrentTable.Rows(i - 1)("Email") = txtsEmail.Text
                    dtCurrentTable.Rows(i - 1)("Phone") = txtSPhone.Text
                    '  dtCurrentTable.Rows(i - 1)("Company") = boxCompany.Text
                    rowIndex += 1
                Next
                'add the rows to the current data table
                dtCurrentTable.Rows.Add(drCurrentRow)
                ViewState("CurrentTable") = dtCurrentTable
                grdStudents.DataSource = dtCurrentTable
                grdStudents.DataBind()
            End If
        Else
            '  Response.Write("ViewState is null") 'needs to chnage the code to something else 
            lbls.Text = "no rows"
        End If
        'Set Previous Data on Postbacks
        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
                grdStudents.DataSource = dt
                grdStudents.DataBind()
                If dt.Rows.Count > 0 Then
                    For i As Integer = 0 To dt.Rows.Count - 1
                        'Dim lblSFname As Label = DirectCast(grdStudents.Rows(rowIndex).Cells(1).FindControl("Sfname"), Label)
                        'Dim lblSLname As Label = DirectCast(grdStudents.Rows(rowIndex).Cells(2).FindControl("SLname"), Label)
                        'Dim lblSEmail As Label = DirectCast(grdStudents.Rows(rowIndex).Cells(3).FindControl("SEmail"), Label)
                        'Dim lblSPhone As Label = DirectCast(grdStudents.Rows(rowIndex).Cells(4).FindControl("SPhone"), Label)

                        '    Dim boxCompany As TextBox = DirectCast(grdStudents.Rows(rowIndex).Cells(5).FindControl("txtCompany"), TextBox)
                        grdStudents.Rows(i).Cells(1).Text = dt.Rows(i)("Fname").ToString()
                        grdStudents.Rows(i).Cells(2).Text = dt.Rows(i)("Lname").ToString()
                        grdStudents.Rows(i).Cells(3).Text = dt.Rows(i)("Email").ToString()
                        grdStudents.Rows(i).Cells(4).Text = dt.Rows(i)("Phone").ToString()
                        'boxCompany.Text = dt.Rows(i)("Company").ToString()
                        rowIndex += 1
                    Next
                End If
            Else
                lbls.Visible = True
                lbls.Text = "error"
            End If
        End If
    End Sub
Protected Sub ButtonAdd_Click(sender As Object, e As EventArgs)
                   AddNewRowToGrid()
                     End Sub

网格上显示的数据正在重复,这意味着不会将我输入的动态值添加到文本框中。你们中的任何人都可以看一下吗? (附上gridview显示的屏幕截图)

以下网址是当前网格视图显示的屏幕截图:

https://ebiztest.cciwa.com/Ebusiness/images/grdView.gif

1 个答案:

答案 0 :(得分:0)

我把一切都搞定了......

从addnewrowtogrid方法中删除了for循环

干杯