By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,492 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

Programmatically Created DataGrid Template Columns

P: n/a
I am creating template columns programmatically. I have read the msdn
article on this and I'm so close. Article:
http://msdn.microsoft.com/library/de...mmatically.asp

I have narrowed down the problem comes in when the _DataBinding
handler is called and the literal text is being assigned, but I can't
figure out beyond that.

The main problem is that "dGrid_Edit" does not get called when I have
all the code in "DataGridTemplate_DataBinding" there. If it is not
there, it does get called, but of course data is not bound correctly.
It's supposed to be called when I click on the "edit" image, but when
that code is there the datagrid doesn't even show up on postback, but
I know the CreateDataGrid() sub is being called.

Thanks in advance!

Nicole

Here is pertinent code:

'Template class
Public 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
lc.Text = columnName
AddHandler lc.DataBinding, AddressOf
DataGridTemplate_DataBinding
container.Controls.Add(lc)
Case ListItemType.EditItem
Dim tb As New TextBox
tb.Text = ""
tb.ID = columnName
container.Controls.Add(tb)
Case ListItemType.Footer
lc.Text = "<I>Footer</I>"
container.Controls.Add(lc)
End Select
End Sub

Sub DataGridTemplate_DataBinding(ByVal sender As Object, ByVal
e As System.EventArgs)
Dim container As DataGridItem
Dim lc As Literal

lc = CType(sender, Literal)
container = CType(lc.NamingContainer, DataGridItem)
If Not IsDBNull(DataBinder.Eval(container.DataItem,
lc.Text)) Then
lc.Text = DataBinder.Eval(container.DataItem,
lc.Text)
End If
End Sub

End Class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
mstrOption = Request.QueryString("Option")
BuildHeaderTable()
CreateDataGrid()

If Not Page.IsPostBack Then
BindGrid()
End If

End Sub

Private Sub CreateDataGrid()
'This procedure creates the data grid
Dim dsData As DataSet
Dim objProj As New RevenueObjects.BusinessObjects.Admin
Dim dCol As DataColumn

'Depending on what user is editing, set dataset
Select Case mstrOption
Case "DataElements"
dsData = objProj.LoadDataElements()

End Select

'Set datagrid properties
dGrid.BorderWidth = Unit.Pixel(2)
dGrid.CellPadding = 10
dGrid.GridLines = GridLines.Both
dGrid.BorderColor = ConvertHexToSystemColor("0099cc")
dGrid.ShowHeader = True
dGrid.AutoGenerateColumns = False
dGrid.SelectedItemStyle.BackColor = Color.Yellow
dGrid.AlternatingItemStyle.BackColor =
ConvertHexToSystemColor("0099cc")

'Allow edit
Dim editCol As New EditCommandColumn
editCol.EditText = GetImageText("Edit")
editCol.CancelText = GetImageText("Cancel")
editCol.UpdateText = GetImageText("Update")
editCol.ItemStyle.Wrap = False
editCol.ItemStyle.Width = Unit.Pixel(100)
dGrid.Columns.Add(editCol)

'Add template columns
Dim colTemplate As TemplateColumn

'Add a template column for each datafield in the dataset
For Each dCol In dsData.Tables(0).Columns
colTemplate = New TemplateColumn
colTemplate.HeaderTemplate = New
DataGridTemplate(ListItemType.Header, dCol.ColumnName)
colTemplate.ItemTemplate = New
DataGridTemplate(ListItemType.Item, dCol.ColumnName)
colTemplate.EditItemTemplate = New
DataGridTemplate(ListItemType.EditItem, dCol.ColumnName)
'colTemplate.FooterTemplate = New
DataGridTemplate(ListItemType.Footer, dCol.ColumnName)

dGrid.Columns.Add(colTemplate)
Next

End Sub

Private Sub BindGrid()
'Binds data to the datagrid
Dim dsData As DataSet
Dim objProj As New RevenueObjects.BusinessObjects.Admin

Select Case mstrOption
Case "DataElements"
dsData = objProj.LoadDataElements()

End Select

dGrid.DataSource = dsData
dGrid.DataKeyField = "DataElementID"
dGrid.DataBind()
End Sub

Sub dGrid_Edit(ByVal Sender As Object, ByVal E As
DataGridCommandEventArgs) Handles dGrid.EditCommand
dGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi Nicole
The main problem is that "dGrid_Edit" does not get called when I have
all the code in "DataGridTemplate_DataBinding" there. If it is not
there, it does get called, but of course data is not bound correctly.
It's supposed to be called when I click on the "edit" image, but when
that code is there the datagrid doesn't even show up on postback, but
I know the CreateDataGrid() sub is being called.


The correct way to add DataGridColumn at runtime is to create and add the
column in the "Init" method.
Otherwise the ViewState is not correctly loaded and than some events not
fire!

You should try to move the "CreateDataGrid()" in the init.

Ciao
Giorgio
Nov 17 '05 #2

P: n/a
Thanks Giorgio!! That did the trick! Wow, that was driving me crazy!

Ciao,
Nicole

"Giorgio Parmeggiani" <gi******@gipadotnet.com> wrote in message news:<ew**************@TK2MSFTNGP11.phx.gbl>...
Hi Nicole
The main problem is that "dGrid_Edit" does not get called when I have
all the code in "DataGridTemplate_DataBinding" there. If it is not
there, it does get called, but of course data is not bound correctly.
It's supposed to be called when I click on the "edit" image, but when
that code is there the datagrid doesn't even show up on postback, but
I know the CreateDataGrid() sub is being called.


The correct way to add DataGridColumn at runtime is to create and add the
column in the "Init" method.
Otherwise the ViewState is not correctly loaded and than some events not
fire!

You should try to move the "CreateDataGrid()" in the init.

Ciao
Giorgio

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.