How to populate a griview programatically

Posted on 2011-10-25
Last Modified: 2013-11-05
How can I populate a gridview programatically in ASP.NET using VB ?

I want to be able to loop through rows and columns and set each cell value

Question by:kevindockerty
    LVL 28

    Accepted Solution

    assuming you have an array of data:

    Dim myNewRow As GridViewRow

    For each row As string[] in MyArray
        myNewRow = new GridViewRow()
        for each column As string in row
    LVL 18

    Assisted Solution

    The problem of a dynamic control is surviving postbacks. If all you want is to display data, then it shouldn't be a problem. If you want your data to be manipulated via dynamic textboxes and buttons, it's more complicated. They have to be declared at each page load. In the case of buttons, you have to declare and add an event handler at page load, then dispose of them and create them again in your gridview. All in all, it becomes quite a headache, as I've found out to my dismay.

    But I found that this link helped me:

    Also, see if this helps:
    LVL 3

    Author Comment

    I found a way to do it

    You need to build a data table in memory and then bind the gridview to this
    In my example I am reading the windows tasklist from a file

            Dim dt As DataTable = New DataTable()
            Dim ThisLine As String
            Dim objStreamReader As StreamReader
            Dim dr As DataRow
            ' Create Columns
            dt.Columns.Add("ImageName", System.Type.GetType("System.String"))
            dt.Columns.Add("PID", System.Type.GetType("System.String"))
            dt.Columns.Add("Memory", System.Type.GetType("System.String"))
            dt.Columns.Add("Username", System.Type.GetType("System.String"))
            dt.Columns.Add("CPU", System.Type.GetType("System.String"))
            'Get a StreamReader class that can be used to read the file
            objStreamReader = File.OpenText(fn)
            ThisLine = objStreamReader.ReadLine()
            Do While Not objStreamReader.EndOfStream
                ThisLine = objStreamReader.ReadLine()

                Dim fields() As String = Split(ThisLine,  "," )
                dr = dt.NewRow
                dr("ImageName") = fields(0)
                dr("PID") = fields(1)
                dr("Memory") = fields(4)
                dr("UserName") = fields(5)
                dr("CPU") = fields(6)
    LVL 3

    Author Comment

    and then I bind dt ( created above ) to the datagrid
    LVL 3

    Author Closing Comment

    I included my own solution as this was the one that worked

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 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

    22 Experts available now in Live!

    Get 1:1 Help Now