GridView with Template columns - add row


I have an GridView with 30 template text columns.
How do I programatically add a row to this?
The code below shows an example of how I do this with a GridView
Is it possible to do something similar?
What code would I use?

  Dim newRow As New DataGridViewRow()


            newRow.Cells(0).Value = CStr(oBatchID)
            newRow.Cells(1).Value = CStr(oTransactionID)
            newRow.Cells(2).Value = CStr(oLinkID)
            'newRow.Cells(3).Value = CStr(Format(dTransactionDate, "yyy-MMM-dd"))
            'newRow.Cells(4).Value = CStr(Format(dCaptureDate, "yyy-MMM-dd"))
            'newRow.Cells(3).Value = CStr(dTransactionDate.Year) & "-" & CStr(dTransactionDate.Month) & "-" & CStr(dTransactionDate.Day)
            'newRow.Cells(4).Value = CStr(dCaptureDate.Year) & "-" & CStr(dCaptureDate.Month) & "-" & CStr(dCaptureDate.Day)
            newRow.Cells(3).Value = sTransactionDate
            newRow.Cells(4).Value = sCaptureDate
            newRow.Cells(5).Value = sPPeriod
            newRow.Cells(6).Value = sFinYear
            newRow.Cells(7).Value = sGDC
            newRow.Cells(8).Value = sReference
            newRow.Cells(9).Value = sDescription
            newRow.Cells(10).Value = sAccountNumber
            newRow.Cells(11).Value = sLinkAcc
            newRow.Cells(12).Value = oAmount
            newRow.Cells(13).Value = CStr(oTaxType)
            newRow.Cells(14).Value = CStr(oTaxAmount)
            newRow.Cells(15).Value = sUserID
            newRow.Cells(16).Value = CStr(oSupplierID)
            newRow.Cells(17).Value = CStr(oEmployeeID)
            newRow.Cells(18).Value = sDescription2
            newRow.Cells(19).Value = sDescription3
            newRow.Cells(20).Value = sDescription4
            newRow.Cells(21).Value = sDescription5

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
The code you posted would easily add a row.  You obviously need to specify your own values for each cell though.

I've used the sample provided by Alan, and unfortunately had trouble getting it to persist across postbacks.  Other developers have also complained about this, as by default the rest of the gridview persists.

To overcome this, I would recommend adding the row to your datasource before
binding it.   E.g. add a row to the datatable/dataset which is really easy.

That way the data types are enforced (e.g. doubles, integers and strings) before being bound (binding data itself can change the appearing of the underlying data don't forget).
Alan WarrenApplications DeveloperCommented:
there's a code sample here of a template class to represent a dynamic template column.
The following code example demonstrates how to use the constructor to dynamically add a TemplateField field column to a GridView control.

I expect the class could accommodate your current code.

Alan ";0)
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
IF you need help with add a row to the datasource, just open a new question then post a link here so I get notified on email

Good luck.... :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.