[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


ASP.net GridView with Template columns - add row

Posted on 2012-08-18
Medium Priority
Last Modified: 2012-08-20

I have an ASP.net 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 VB.net 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

Question by:Murray Brown
  • 2
LVL 26

Expert Comment

by:Alan Warren
ID: 38308743
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)
LVL 25

Accepted Solution

Rouchie earned 2000 total points
ID: 38310862
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).

Author Closing Comment

by:Murray Brown
ID: 38310928
Thanks very much
LVL 25

Expert Comment

ID: 38310937
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.... :-)

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month19 days, 7 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question