When you bind an array to a datagrid, what's the field name?

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default When you bind an array to a datagrid, what's the field name?

    In ASP.NET v2.0, I can successfully bind a generic datagrid to a 1 dimensional
    array. But, now I want to customise the column header. I think I need to
    make the first column (the one having data in the array) a "BoundColumn" and
    set the HeaderText property, but the problem is that the BoundColumn wants
    a DataField name. There is none! So how do I bind this one column to the
    array column when the array doesn't have column names?

    Example
    =======

    Code
    ----
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim test() As String = New String() {"one", "two", "three", "four","five"}
    Me.DataGrid1.DataSource = test
    Me.DataGrid1.DataBind()
    End Sub

    Results
    -------
    Item
    one
    two
    three
    four
    five

    It looks like the column name "Item" should work... but it doesn't!

    Thanks,
    Jon.
    Jon Edney Guest

  2. Similar Questions and Discussions

    1. #36961 [Com]: mssql_bind will not bind an image field
      ID: 36961 Comment by: rnerovich at gmail dot com Reported By: nerovichr at hotpop dot com Status: Open Bug...
    2. Howto bind CheckBox to the datagrid/ Then update the database field when the checkbox is clicked.
      I am trying to update the database field when the checkbox is clicked. I am trying to modified the following solution but.. got stuck on the...
    3. CFGRID with bind to text field --- IS IT ME????!
      Ok...I'll keep it simple. I cannot get the binding feature to work in CF with cfgrid and cfform together. I am taking the example from the...
    4. custom usercontrol inside of datagrid - loses its state/viewstate on re-bind/postback of the datagrid
      I have a simple usercontrol, a datepicker which contains 3 dropdownlist , it resides inside a datagrid column and i set the selecteddate property of...
    5. Bind Paramaters To An Array
      Using flash remoting I have a stack of paramaters and then I have an array items I want to bind them to. Currently I just use the ...
  3. #2

    Default Re: When you bind an array to a datagrid, what's the field name?

    You'll have to either set a HeaderTemplate for each column or handle the
    DataBound event for the header row and change it manually.

    -Brock
    DevelopMentor
    [url]http://staff.develop.com/ballen[/url]
    > In ASP.NET v2.0, I can successfully bind a generic datagrid to a 1
    > dimensional
    > array. But, now I want to customise the column header. I think I
    > need to
    > make the first column (the one having data in the array) a
    > "BoundColumn" and
    > set the HeaderText property, but the problem is that the BoundColumn
    > wants
    > a DataField name. There is none! So how do I bind this one column to
    > the
    > array column when the array doesn't have column names?
    > Example
    > =======
    > Code
    > ----
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > Dim test() As String = New String() {"one", "two", "three",
    > "four","five"}
    > Me.DataGrid1.DataSource = test
    > Me.DataGrid1.DataBind()
    > End Sub
    > Results
    > -------
    > Item
    > one
    > two
    > three
    > four
    > five
    > It looks like the column name "Item" should work... but it doesn't!
    >
    > Thanks,
    > Jon.

    Brock Allen Guest

  4. #3

    Default Re: When you bind an array to a datagrid, what's the field name?

    Hi,

    Thanks for your email.

    I have tried setting the column header on the DataBound event of the Grid,
    but it doesn't believe the automatically generated columns exist. I get an
    out of bounds error message. I've checked the code using normally bound
    fields, and it works fine.

    Could you give me a bit more info on the column template suggestion? I'm not
    entierly sure what you are suggesting.

    Thanks,
    Jon.


    "Brock Allen" wrote:
    > You'll have to either set a HeaderTemplate for each column or handle the
    > DataBound event for the header row and change it manually.
    >
    > -Brock
    > DevelopMentor
    > [url]http://staff.develop.com/ballen[/url]
    >
    > > In ASP.NET v2.0, I can successfully bind a generic datagrid to a 1
    > > dimensional
    > > array. But, now I want to customise the column header. I think I
    > > need to
    > > make the first column (the one having data in the array) a
    > > "BoundColumn" and
    > > set the HeaderText property, but the problem is that the BoundColumn
    > > wants
    > > a DataField name. There is none! So how do I bind this one column to
    > > the
    > > array column when the array doesn't have column names?
    > > Example
    > > =======
    > > Code
    > > ----
    > > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs) Handles MyBase.Load
    > > Dim test() As String = New String() {"one", "two", "three",
    > > "four","five"}
    > > Me.DataGrid1.DataSource = test
    > > Me.DataGrid1.DataBind()
    > > End Sub
    > > Results
    > > -------
    > > Item
    > > one
    > > two
    > > three
    > > four
    > > five
    > > It looks like the column name "Item" should work... but it doesn't!
    > >
    > > Thanks,
    > > Jon.
    >
    >
    >
    Jon Edney Guest

  5. #4

    Default Re: When you bind an array to a datagrid, what's the field name?

    Just make your template like this (instead of a BoundColumn):

    <Columns>
    <asp:TemplateColumn>
    <HeaderTemplate>Your Header Goes Here</HeaderTemplate>
    <ItemTemplate><%# Container.DataItem %></ItemTemplate>
    </asp:TemplateColumn>
    </Columns>

    -Brock
    DevelopMentor
    [url]http://staff.develop.com/ballen[/url]
    > Hi,
    >
    > Thanks for your email.
    >
    > I have tried setting the column header on the DataBound event of the
    > Grid, but it doesn't believe the automatically generated columns
    > exist. I get an out of bounds error message. I've checked the code
    > using normally bound fields, and it works fine.
    >
    > Could you give me a bit more info on the column template suggestion?
    > I'm not entierly sure what you are suggesting.
    >
    > Thanks,
    > Jon.
    > "Brock Allen" wrote:
    >
    >> You'll have to either set a HeaderTemplate for each column or handle
    >> the DataBound event for the header row and change it manually.
    >>
    >> -Brock
    >> DevelopMentor
    >> [url]http://staff.develop.com/ballen[/url]
    >>> In ASP.NET v2.0, I can successfully bind a generic datagrid to a 1
    >>> dimensional
    >>> array. But, now I want to customise the column header. I think I
    >>> need to
    >>> make the first column (the one having data in the array) a
    >>> "BoundColumn" and
    >>> set the HeaderText property, but the problem is that the BoundColumn
    >>> wants
    >>> a DataField name. There is none! So how do I bind this one column to
    >>> the
    >>> array column when the array doesn't have column names?
    >>> Example
    >>> =======
    >>> Code
    >>> ----
    >>> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    >>> System.EventArgs) Handles MyBase.Load
    >>> Dim test() As String = New String() {"one", "two", "three",
    >>> "four","five"}
    >>> Me.DataGrid1.DataSource = test
    >>> Me.DataGrid1.DataBind()
    >>> End Sub
    >>> Results
    >>> -------
    >>> Item
    >>> one
    >>> two
    >>> three
    >>> four
    >>> five
    >>> It looks like the column name "Item" should work... but it doesn't!
    >>> Thanks,
    >>> Jon

    Brock Allen Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139