How can i loop through columns and name them with customised names at runtime
I would want the header names to be picked from table upon load dynamically
I wanted the cell value to be looped and automatically adjust to the columns available
how can i do this
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
GridView1.HeaderRow.Cells(0).Text = "Bookcode"
GridView1.HeaderRow.Cells(1).Text = "Kampala"
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("SELECT CustomerId, ContactName, City, Country FROM Customers1")
Using sda As New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
this is the same
GridView1.HeaderRow.Cells(0).Text = "Bookcode"
GridView1.HeaderRow.Cells(1).Text = "Kampala"
below is my table
Code Location
0001 Kampala
0002 Maskas