DataGrid Columns Created at Run time

Discussion in 'ASP .Net' started by Gary Blakely, Oct 7, 2005.

  1. Gary Blakely

    Gary Blakely Guest

    In the program below, the web page has dataGrid1. the only thing that has
    been done to it at design time is to check the "Create columns automatically
    at runtime" checkbox - nothing else.

    The code below does indeed create the visual grid as expected. Furthermore
    the Cell contents and Item count all exist and contain expected values.
    However there is no columns array !! After the databind, for instance,
    DataGrid1.Columns(0) does not exist. There is therefore, no way to even
    reference the HeaderText (which visually shows) or any other attribute of
    the column.

    Why is this?


    Public Class WebForm1
    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    Dim SQLStr As String = "Select FirstName, LastName From
    Employees"
    Dim myConnection As New System.Data.SqlClient.SqlConnection
    myConnection.ConnectionString = _
    "workstation id=myMachine;user id=sa;data source=myMachine;" & _
    "persist security info=True;initial
    catalog=NorthWind;password=xxxxx"
    myConnection.Open()
    Dim cmd As New System.Data.SqlClient.SqlCommand(SQLStr,
    myConnection)
    Dim da As New System.Data.SqlClient.SqlDataAdapter
    da.SelectCommand = cmd
    Dim dt As New DataTable
    da.Fill(dt)
    DataGrid1.DataSource = dt
    DataGrid1.DataBind()

    'at this point DataGrid1.Columns(0) does not exist
    'however...
    'DataGrid1.items(0).cells(0).text = "Nancy"
    'DataGrid1.items.Count = 9

    End If
    End Sub

    End Class
     
    Gary Blakely, Oct 7, 2005
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.