How to populate a griview programatically

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

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

strickddConnect With a Mentor Commented:
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
CluskittConnect With a Mentor Commented:
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:
kevindockertyAuthor Commented:
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)
kevindockertyAuthor Commented:
and then I bind dt ( created above ) to the datagrid
kevindockertyAuthor Commented:
I included my own solution as this was the one that worked
All Courses

From novice to tech pro — start learning today.