[RESOLVED] Adding rows to GridView programatically
Today is the first day i hav to use the new ASP.NET 2.0 GridView control. After searching all over the place, it seems that one cannot programatically add rows to GridView like one can with DataGrid. Why can`t i add rows programatically? And is it possible with some trick to add rows? Makes me feel like wanting to go back to using DataGrid
You can populate a 'DataTable' with your data then bind that to the gridview (you can programatically add rows to the datatable).
i.e
Code:
Imports System.Data
Private Sub BindData()
gridview1.DataSource = GetData()
gridview1.DataBind()
End Sub
Protected Function FormatDataTable() as DataTable
Dim dt as DataTable = New DataTable()
' Create Columns
dt.Columns.Add("id", System.Type.GetType("System.String"))
dt.Columns.Add("text", System.Type.GetType("System.String"))
Return dt
End Function
Protected Function GetData() as DataTable
Dim dt as DataTable = FormatDataTable()
Dim dr as DataRow
' Populate the datatable with your data (put this in appropriate loop)
dr = dt.NewRow
dr("id") = "1"
dr("txt") = "Hi"
' Add the row
dt.Rows.Add(dr)
dt.AcceptChanges
Return dt
End Function
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
Bookmarks