?
Solved

ASP.NET DataGrid: Access Datasource from dynamically created TemplateColumn

Posted on 2009-04-25
1
Medium Priority
?
269 Views
Last Modified: 2012-05-06
Hello,

I am trying to dynamically generate a DataGrid based on user-selected columns and I am having a bit of trouble generating a TemplateColumn.  Using code from MSDN I was able to create a DataGridTemplate column from my code-behind but the code that was provided merely outputs the column name as the column value, and not the actual value itself.  So far I have been unable to access the datasource that is  bound to the DataGrid that I am dynamically generating.  When I try to set the value to DataBinder.Eval(container, columnName) I am greeted with the error that the column does not exist on the datasource and I the name I am using is correct.  Could anyone let me know what it is I have to do to access that datasource?

TIA,

Mike C

The code provided is for my slightly modified DataGridTemplate code


Private Class DataGridTemplate
        Implements ITemplate
        Dim templateType As ListItemType
        Dim columnName As String
 
        Sub New(ByVal type As ListItemType, ByVal ColName As String)
            templateType = type
            columnName = ColName
        End Sub
 
        Sub InstantiateIn(ByVal container As Control) _
           Implements ITemplate.InstantiateIn
            Dim lc As New Literal()
            Select Case templateType
                Case ListItemType.Header
                    lc.Text = "<B>" & columnName & "</B>"
                    container.Controls.Add(lc)
                Case ListItemType.Item
                    Dim lb As New LinkButton
                    lb.CommandName = "FilterRoster"
                    lb.CommandArgument = columnName
                    lb.Text = columnName 'This is where I need the actual column value to go
                    container.Controls.Add(lb)
                Case ListItemType.EditItem
                    Dim tb As New TextBox()
                    tb.Text = ""
                    container.Controls.Add(tb)
                Case ListItemType.Footer
                    lc.Text = "<I>Footer</I>"
                    container.Controls.Add(lc)
            End Select
        End Sub
    End Class

Open in new window

0
Comment
Question by:MClarkCCR
1 Comment
 
LVL 3

Accepted Solution

by:
MClarkCCR earned 0 total points
ID: 24233442
Nevermind,

I was able to figure it out.  Instead of trying to bind the value directly in my ItemTemplate I created an EventHandler to my data bound control (in this case a LinkButton).

Instead of:

                Case ListItemType.Item
                    Dim lb As New LinkButton
                    lb.CommandName = "FilterRoster"
                    lb.CommandArgument = columnName
                    lb.Text = columnName 'This is where I need the actual column value to go
                    container.Controls.Add(lb)

I now have:

                Case ListItemType.Item
                    Dim lb As New LinkButton
                    lb.CommandName = "FilterRoster"
                    lb.CommandArgument = columnName
                    AddHandler lb.DataBinding, AddressOf lb_DataBinding
                    container.Controls.Add(lb)

And my code for my EventHandler:

        Protected Sub lb_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
            Dim lbData As LinkButton = CType(sender, LinkButton)
            Dim container As DataGridItem = CType(lbData.NamingContainer, DataGridItem)
            lbData.Text = DataBinder.Eval(container.DataItem, columnName)
        End Sub

That's all it takes to get the value from the datasource!
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

839 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