Solved GridView with Template columns - add row

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

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

Question by:murbro
    LVL 26

    Expert Comment

    by:Alan Warren
    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

    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

    Thanks very much
    LVL 25

    Expert Comment

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now