?
Solved

Adding ItemTemplate to a TemplateColumn at run time

Posted on 2004-09-21
5
Medium Priority
?
4,614 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1600 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

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