在gridview中添加新行

时间:2017-06-07 04:42:25

标签: asp.net vb.net gridview

更新

从这个i绑定gridview

Public Function uplif() As DataTable
        Dim dt As New DataTable()
        gridvieew1.Visible = True

        Try

            dt.Columns.AddRange(New DataColumn(1) {New DataColumn("ID", GetType(Integer)),
                                                   New DataColumn("Name", GetType(String))}
            Dim Content As String = Request.Form(textbox1.UniqueID)
            For Each row As String In Content .Split(ControlChars.Lf)
                If Not String.IsNullOrEmpty(row) Then
                    dt.Rows.Add()
                    Dim i As Integer = 0
                    For Each cell As String In row.Split(ControlChars.Tab)
                        If i > 1 Then
                            labelmessage.Text = "More than 2 columns not Allowed !!"
                            textbox1.Text = ""
                        Else
                            If cell.Trim() = "" Then
                                cell = "0"
                            End If
                            dt.Rows(dt.Rows.Count - 1)(i) = cell
                            i += 1
                        End If
                    Next
                End If
            Next
             gridvieew1.DataSource = dt
             gridvieew1.DataBind()

        Catch Ex As Exception
            labelmessage.CssClass = "alertNo"
            labelmessage.Text = Ex.Message
        End Try
        Return dt
    End Function

  Protected Sub PasteToGridView(sender As Object, e As EventArgs)
        uplif()

    End Sub

现在当我尝试添加像这样的新行

Private Sub AddNewRowToGrid()
        Dim dt As DataTable = uplif()
        Dim NewRow As DataRow = dt.NewRow()
        dt.Rows.Add(NewRow)
         gridvieew1.DataSource = dt
         gridvieew1.DataBind()
    End Sub

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        AddNewRowToGrid()
    End Sub

这显示了相同的输出,例如添加新行并替换已经在gridview中的先前数据,这也显示错误 对象引用未设置为对象的实例。

点击后添加新行

ID Name

我喜欢这样的地方

ID Name
1  abc
2  def
(here new empty row when click on button )

2 个答案:

答案 0 :(得分:0)

正如您对帖子的评论所指出的那样,问题在于以下两个方面:

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin</artifactId>
            <version>1.21.0</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-ui</artifactId>
            <version>1.21.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-server</artifactId>
            <version>1.21.0</version>
        </dependency>
        <dependency>
            <groupId>io.zipkin.java</groupId>
            <artifactId>zipkin-autoconfigure-storage-elasticsearch</artifactId>
            <version>1.21.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.integration</groupId>
            <artifactId>spring-integration-jmx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>
    </dependencies>

这会创建一个全新的Dim dt As New DataTable() ... gridview1.DataSource = dt 并将网格视图绑定到它。您不会复制数据,也不会更改现有的gridview表。

这里正确的解决方法是从gridview的DataSource获取DataTable并对其进行所需的任何更改。不要创建新的DataTable

答案 1 :(得分:0)

获取已分配的DataSource,然后添加Row。不要添加列,因为它已经存在于您的数据源

 Private Sub AddNewRowToGrid()
        Dim dt As gridview1.DataSource       
        Dim NewRow As DataRow = dt.NewRow()
        dt.Rows.Add(NewRow)
        gridview1.DataSource = dt
        gridview1.DataBind()
    End Sub

 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    AddNewRowToGrid()
End Sub