Solved

Adding ItemTemplate to a TemplateColumn at run time

Posted on 2004-09-21
5
4,591 Views
Last Modified: 2012-08-13
Here we go brain-trust,
I am trying to add a ItemTemplate containing a hyperlink to a datagrid during run time.
I keep getting this error:
Object reference not set to an instance of an object

On this line:
Line 304:            .ItemTemplate.InstantiateIn(hlChoose)

In this code:
    Private Function POGrid(ByVal ds As DataSet)

        Dim bdPO As New BoundColumn
        bdPO.DataField = "t$pdno"
        bdPO.HeaderText = "PO#"
        'SO Column
        Dim bdSO As New BoundColumn
        bdSO.DataField = "t$orno"
        bdSO.HeaderText = "SO#"
        'Request Column
        Dim bdRequest As New BoundColumn
        bdRequest.DataField = "t$ddta"
        bdRequest.HeaderText = "Planned Ship Date"
        'Status Column

        'Link Column
        Dim tcLink As New TemplateColumn
        Dim hlChoose As New HyperLink
        Dim strHyper As String = "default.ecm.aspx?so="
        hlChoose.NavigateUrl = strHyper
        hlChoose.ID = "hlChoose"
        hlChoose.Text = "Correct"

        With tcLink
            .HeaderText = "Option"
            .ItemTemplate.InstantiateIn(hlChoose)
        End With

        tcLink.HeaderText = "Go To"
        With Me.dgExisting
            .AutoGenerateColumns = False
            .Columns.Add(bdPO)
            .Columns.Add(bdSO)
            .Columns.Add(bdRequest)
            .Columns.Add(tcLink)
            .DataSource = ds
            .DataBind()
            .Visible = True
            '.AllowPaging = True
            '.PageSize = 15

        End With

Along with that how do I assign the Field "t$orno" to Hyerlink.NaviagteURL
As you can see the dataset is being passed to this function. (Dataset filled by SQLDataAdapter)

I am pulling my hair out...lol
0
Comment
Question by:bmickey
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:gsiric
ID: 12122481
HI,
you cant create template column on this way.
InstantiateIn is member of ITemplate interface, and method is called by the framework.
Parameter of InstantiateIn is not control which will be instatiated in container.
This is reference to the container.
This reference is used in implementig InstantiateIn when you implement ITemplate interface.

For details see:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingtemplatesprogrammaticallyindatagridcontrol.asp


0
 

Author Comment

by:bmickey
ID: 12122918
I am confused, I saw an answer to a question that was very similar. Why will it work for him and not me.
Q_21016234.html
The big differnce I see is that I am using ADO.Net (SQLDataAdapter)  and he is using ADO (ADODB.Recordset ).
0
 

Author Comment

by:bmickey
ID: 12122993
What if I use a hyperlinkColumn?
0
 
LVL 7

Accepted Solution

by:
gsiric earned 400 total points
ID: 12123362
In this question his have CheckColumnTemplate which implement ITemplate.
And he define new repeater at runtime and instatiate Template in this repeater.


Repeater rptr = new Repeater();
CheckColumnTemplate col = new CheckColumnTemplate();
col.InstantiateIn(rptr);

you try to instantiate TemplateColumn in hyperlink.

You need to do (in C#):

public class HyperlinkColumnTemplate : ITemplate
{
    public void InstantiateIn(Control container)
  {
            HyperLink hl = new HyperLink();
            container.Controls.Add(hl);
   }
}



TemplateColumn tc = new TemplateColumn();
tc.ItemTemplate = new HyperLinkColumnTemplate();
...

and you dont need to call InstantiateIn.


0
 

Author Comment

by:bmickey
ID: 12124131
I was able to use HyperLinkColumn and a minor change to my my SQL Query to get the desired results at this time.

VB.Net Code
        Dim tcLink As New HyperLinkColumn

        With tcLink
            .HeaderText = "Go To"
            .DataNavigateUrlField = "address"
            .Text = "Go"
           
        End With

Trans-Act:
select t$orno,t$cuno,t$item,t$csgs,t$ddta,t$cprj,t$pdno,t$mitm,t$dscd, ('default.ecm.aspx?so=' + Convert(varchar(8),t$orno)) as Address from

Thank you for your help and the code. Knowledge I am sure I will use later
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now