从网格中删除空白行

时间:2017-06-17 06:30:17

标签: asp.net vb.net gridview dynamically-generated

我尝试在网格中动态添加记录

Private Sub AddToGrid()
    If ViewState("table") IsNot Nothing Then
        Dim dtable As DataTable = DirectCast(ViewState("table"), DataTable)
        Dim drow As DataRow = Nothing

            For i As Integer = 0 To dtable.Rows.Count
                drow = dtable.NewRow()
                drow("ID") = TEXT_1.Text
                drow("Name") = ddl_name.SelectedItem.Text
                drow("Type") = ddl_type.SelectedItem.Text
            Next
            dtable.Rows.Add( drow)
            ViewState("table") =dtable
            gridv.DataSource = dtable
            gridv.DataBind()
        End If

End Sub

Private Sub AddDefaultFirstRecord()  
    Dim dt As New DataTable()
    Dim dr As DataRow
    dt.TableName = "table"
    dt.Columns.Add(New DataColumn("ID"))
    dt.Columns.Add(New DataColumn("Name"))
    dt.Columns.Add(New DataColumn("Type"))
    dr = dt.NewRow()
    dt.Rows.Add(dr)
    ViewState("table") = dt
    gridv.DataSource = dt
    gridv.DataBind()
End Sub

Protected Sub Add_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add.Click
    AddToGrid()
End Sub

并在页面加载时调用此

AddDefaultFirstRecord() 

当我添加记录时,网格有空白行,所以我如何删除这一行

像这样

       ID       Name     Type
Delete               
Delete  92615   Din     tYPE1

所以如何删除这个空的空白行我没有添加这个,当我点击添加然后记录这个空行我添加

1 个答案:

答案 0 :(得分:2)

AddDefaultFirstRecord()中删除以下代码:

dr = dt.NewRow()
dt.Rows.Add(dr)