Create TemplateColumn on datagrid exclusively with code behind

Discussion in 'ASP .Net' started by Ricardo, Apr 11, 2005.

  1. Ricardo

    Ricardo Guest

    Hi,

    How can I insert a TemplateColumn on a Datagrid on the fly?

    Basically I'm after how to create a TemplateColumn, add a button and a
    label to its control list and add the TemplateColumn to my Datagrid.

    Something like:

    Dim tc as New TemplateColumn
    tc.Controls.Add(New Button)
    tc.Controls.Add(New Label)
    DataGrid1.Columns.Add(tc)

    Any ideas?

    Thanks
    Ricardo
     
    Ricardo, Apr 11, 2005
    #1
    1. Advertising

  2. Ricardo

    Brock Allen Guest

    In essence the Template is just a class that knows how to do dynamically
    add other controls. So you'd have to write that class. Here'a a Microsoft
    example:

    http://msdn.microsoft.com/library/d...emplatesprogrammaticallyindatagridcontrol.asp

    Here's a diff example that shows some DataBinding:

    http://www.c-sharpcorner.com/Code/2003/June/AddItemTemplateDynamically.asp

    One other approach is to simply have an extra column that you add controls
    to yourself in the DataGrid's ItemDataBoudn event.

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Hi,
    >
    > How can I insert a TemplateColumn on a Datagrid on the fly?
    >
    > Basically I'm after how to create a TemplateColumn, add a button and a
    > label to its control list and add the TemplateColumn to my Datagrid.
    >
    > Something like:
    >
    > Dim tc as New TemplateColumn
    > tc.Controls.Add(New Button)
    > tc.Controls.Add(New Label)
    > DataGrid1.Columns.Add(tc)
    > Any ideas?
    >
    > Thanks
    > Ricardo
     
    Brock Allen, Apr 11, 2005
    #2
    1. Advertising

  3. Ricardo

    David Alpert Guest

    Or, if you can access the DataGrid's OnItemCreated() routine (possibly
    there's an ItemCreated event?) you can modify each row's HTML
    programmtically after it is created and before it is passed to
    OnItemDataBound(). This is a great way to add dynamically created
    controls (or odd patterns of rowspanned cells) and it cleanly
    separates control creation from databinding.

    Check out Dan Wahlin's excellent example on Extending the DataGrid...
    http://www.xmlforasp.net/codeSection.aspx?csID=92

    David

    Brock Allen <> wrote in message news:<>...
    >
    > One other approach is to simply have an extra column that you add controls
    > to yourself in the DataGrid's ItemDataBoudn event.
     
    David Alpert, Apr 11, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]

    Re: Code Behind vs. no code behind: error

    Ben Miller [msft], Jun 27, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    594
    Alphonse Giambrone
    Jun 28, 2003
  2. =?Utf-8?B?VGlncm91?=
    Replies:
    0
    Views:
    522
    =?Utf-8?B?VGlncm91?=
    Nov 27, 2004
  3. =?Utf-8?B?VGlncm91?=
    Replies:
    2
    Views:
    4,922
    =?Utf-8?B?VGlncm91?=
    Nov 30, 2004
  4. AC [MVP MCMS]
    Replies:
    4
    Views:
    385
    Eliyahu Goldin
    Jan 4, 2007
  5. Ricardo
    Replies:
    2
    Views:
    151
    David Alpert
    Apr 11, 2005
Loading...

Share This Page