Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Rows to an Unbound GridView 1

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
I have a grid view on an ASP.net page. I am using it to display info in a multi-column list format.

A user puts data into two text boxes and I want to add the info to the grid view. If they do this two times I should see two rows. My problems...

1. How do I get more than two rows?
2. How do I delete a row? (I had the delete button and it gave an error message when I clicked it.)

Here is my code that I have in a command button...

Code:
        Dim myDataTable As DataTable = New DataTable("AdditionalCodes") 
   
        myDataColumn = New DataColumn
        myDataColumn.DataType = System.Type.GetType("System.String")
        myDataColumn.ColumnName = "hours"
        myDataColumn.Caption = "Hours"
        myDataColumn.Unique = False
        myDataTable.Columns.Add(myDataColumn)

        myDataColumn = New DataColumn
        myDataColumn.DataType = System.Type.GetType("System.String")
        myDataColumn.ColumnName = "proccode"
        myDataColumn.Caption = "Procedure Code"
        myDataColumn.Unique = False
        myDataTable.Columns.Add(myDataColumn)

        myDataRow = myDataTable.NewRow()
        myDataRow("hours") = Me.cbHoursAdd.SelectedValue
        myDataRow("proccode") = Me.cbProcCodeAdd.SelectedValue
        myDataTable.Rows.Add(myDataRow)

        Me.gvAdditionalCodes.DataSource = myDataTable
        Me.gvAdditionalCodes.DataBind()

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Create a sub that calls this portion of your code:
Code:
Sub AddNewRow()
   myDataRow = myDataTable.NewRow()
   myDataRow("hours") = Me.cbHoursAdd.SelectedValue
   myDataRow("proccode") = Me.cbProcCodeAdd.SelectedValue
   myDataTable.Rows.Add(myDataRow)
   Me.gvAdditionalCodes.DataSource = myDataTable
   Me.gvAdditionalCodes.DataBind()
End Sub

Change your original code:
Code:
Hope this puts you in the right direction you want.

Jim
If Not IsPostBack Then
   Dim myDataTable As DataTable = New DataTable("AdditionalCodes") 
   Session("MyDataTable") = myDataTable
Else
   Dim myDataTable As DataTable = Session("MyDataTable")
End If    
   myDataColumn = New DataColumn
   myDataColumn.DataType = System.Type.GetType("System.String")
   myDataColumn.ColumnName = "hours"
   myDataColumn.Caption = "Hours"
   myDataColumn.Unique = False
   myDataTable.Columns.Add(myDataColumn)

   myDataColumn = New DataColumn
   myDataColumn.DataType = System.Type.GetType("System.String")
   myDataColumn.ColumnName = "proccode"
   myDataColumn.Caption = "Procedure Code"
   myDataColumn.Unique = False
   myDataTable.Columns.Add(myDataColumn)

Code:
'On a button click event you can do this:
        AddNewRow()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top