Click here to Skip to main content
11,720,545 members (71,495 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET VB.NET
hi guys,

this is my code for adding row.i only manage load page with 1 row gridview.i want it to load with 10 rows automatically.then after that user can add with 10 more new rows..

Private Sub FirstGridViewRow()
        Dim dt As New DataTable()
        Dim dr As DataRow = Nothing
 
        'Dim item As New GridView()
        dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
        dt.Columns.Add(New DataColumn("Col1", GetType(String)))
        dt.Columns.Add(New DataColumn("Col2", GetType(String)))
        dt.Columns.Add(New DataColumn("Col3", GetType(String)))
        dt.Columns.Add(New DataColumn("Col4", GetType(String)))
        dt.Columns.Add(New DataColumn("Col5", GetType(String)))
        dt.Columns.Add(New DataColumn("Col6", GetType(String)))
        dt.Columns.Add(New DataColumn("Col7", GetType(String)))
        dt.Columns.Add(New DataColumn("Col8", GetType(String)))
        dt.Columns.Add(New DataColumn("Col9", GetType(String)))
        dr = dt.NewRow()
 

        dr("RowNumber") = 1
        dr("Col1") = String.Empty
        dr("Col2") = String.Empty
        dr("Col3") = String.Empty
        dr("Col4") = String.Empty
        dr("Col5") = String.Empty
        dr("Col6") = String.Empty
        dr("Col7") = String.Empty
        dr("Col8") = String.Empty
        dr("Col9") = String.Empty
 
        dt.Rows.Add(dr)
 

 
        ViewState("CurrentTable") = dt
        grvBorang.DataSource = dt
        grvBorang.DataBind()
 

        depohlist.Focus()
        Dim btnAdd As Button = DirectCast(grvBorang.FooterRow.Cells(9).FindControl("ButtonAdd"), Button)
        Page.Form.DefaultFocus = btnAdd.ClientID
 
    End Sub
 

    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 stokereta As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(1).FindControl("stokereta"), TextBox)
                    Dim ddl_jenisroda As DropDownList = DirectCast(grvBorang.Rows(rowIndex).Cells(2).FindControl("ddl_jenisroda"), DropDownList)
                    Dim nosiri As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(3).FindControl("nosiri"), TextBox)
                    Dim saizroda As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(4).FindControl("saizroda"), TextBox)
                    Dim flange As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(5).FindControl("flange"), TextBox)
                    Dim hollow As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(6).FindControl("hollow"), TextBox)
                    Dim ddl_tindakan As DropDownList = DirectCast(grvBorang.Rows(rowIndex).Cells(7).FindControl("ddl_tindakan"), DropDownList)
                    Dim ddl_skid As DropDownList = DirectCast(grvBorang.Rows(rowIndex).Cells(8).FindControl("ddl_skid"), DropDownList)
                    Dim catatan As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(9).FindControl("catatan"), TextBox)
                    drCurrentRow = dtCurrentTable.NewRow()
                    drCurrentRow("RowNumber") = i + 10
 

                    dtCurrentTable.Rows(i - 1)("Col1") = stokereta.Text
                    dtCurrentTable.Rows(i - 1)("Col2") = ddl_jenisroda.SelectedItem.Text
                    dtCurrentTable.Rows(i - 1)("Col3") = nosiri.Text
                    dtCurrentTable.Rows(i - 1)("Col4") = saizroda.Text
                    dtCurrentTable.Rows(i - 1)("Col5") = flange.Text
                    dtCurrentTable.Rows(i - 1)("Col6") = hollow.Text
                    dtCurrentTable.Rows(i - 1)("Col7") = ddl_tindakan.SelectedValue
                    dtCurrentTable.Rows(i - 1)("Col8") = ddl_skid.SelectedValue
                    dtCurrentTable.Rows(i - 1)("Col9") = catatan.Text
 
                    rowIndex += 1
                Next
                dtCurrentTable.Rows.Add(drCurrentRow)
                ViewState("CurrentTable") = dtCurrentTable
 
                grvBorang.DataSource = dtCurrentTable
                grvBorang.DataBind()
 
                Dim txn As TextBox = DirectCast(grvBorang.Rows(rowIndex).Cells(1).FindControl("stokereta"), TextBox)
                txn.Focus()
            End If
        Else
            Response.Write("ViewState is null")
        End If
        SetPreviousData()
    End Sub
 

pls help me.this is very urgent.i have to submit today.
 
thanks in advance
musiw.
Posted 29-Nov-12 15:12pm
musiw713

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi

Can you try to create your datatable as given below. (I didn't compile the code)

Dim dt As New DataTable()
dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
dt.Columns.Add(New DataColumn("Col1", GetType(String)))
dt.Columns.Add(New DataColumn("Col2", GetType(String)))
dt.Columns.Add(New DataColumn("Col3", GetType(String)))
dt.Columns.Add(New DataColumn("Col4", GetType(String)))
dt.Columns.Add(New DataColumn("Col5", GetType(String)))
dt.Columns.Add(New DataColumn("Col6", GetType(String)))
dt.Columns.Add(New DataColumn("Col7", GetType(String)))
dt.Columns.Add(New DataColumn("Col8", GetType(String)))
dt.Columns.Add(New DataColumn("Col9", GetType(String)))
 

Dim oDr As DataRow = dt.NewRow()
dr("RowNumber") = 1
dr("Col1") = String.Empty
dr("Col2") = String.Empty
dr("Col3") = String.Empty
dr("Col4") = String.Empty
dr("Col5") = String.Empty
dr("Col6") = String.Empty
dr("Col7") = String.Empty
dr("Col8") = String.Empty
dr("Col9") = String.Empty
dt.Rows.Add(oDr)
 

oDr = Nothing
oDr = dt.NewRow()
dr("RowNumber") = 2
dr("Col1") = String.Empty
dr("Col2") = String.Empty
dr("Col3") = String.Empty
dr("Col4") = String.Empty
dr("Col5") = String.Empty
dr("Col6") = String.Empty
dr("Col7") = String.Empty
dr("Col8") = String.Empty
dr("Col9") = String.Empty
dt.Rows.Add(oDr)
 
oDr = Nothing
oDr = dt.NewRow()
dr("RowNumber") = 3
dr("Col1") = String.Empty
dr("Col2") = String.Empty
dr("Col3") = String.Empty
dr("Col4") = String.Empty
dr("Col5") = String.Empty
dr("Col6") = String.Empty
dr("Col7") = String.Empty
dr("Col8") = String.Empty
dr("Col9") = String.Empty
dt.Rows.Add(oDr)
 
'---3 rd row
'--4 th row
' --5 thw
'etc..create all your rows like this

dt.AcceptChanges()
grvBorang.DataSource = dt
grvBorang.DataBind()
  Permalink  
Comments
Dominic Abraham at 30-Nov-12 3:43am
   
Your issue solved?
musiw at 30-Nov-12 5:30am
   
TQ dominic for asking. i'd just tried my coding.i make some alteration.seems like yours.but i using for loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 2,129
1 OriginalGriff 884
2 F-ES Sitecore 640
3 Maciej Los 609
4 Richard MacCutchan 540


Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 29 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100