I am creating template columns programmaticall y. I have read the msdn
article on this and I'm so close. Article:
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 "DataGridTempla te_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 DataGridTemplat e
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(B yVal container As Control) _
Implements ITemplate.Insta ntiateIn
Dim lc As New Literal
Select Case templateType
Case ListItemType.He ader
lc.Text = "<B>" & columnName & "</B>"
container.Contr ols.Add(lc)
Case ListItemType.It em
lc.Text = columnName
AddHandler lc.DataBinding, AddressOf
DataGridTemplat e_DataBinding
container.Contr ols.Add(lc)
Case ListItemType.Ed itItem
Dim tb As New TextBox
tb.Text = ""
tb.ID = columnName
container.Contr ols.Add(tb)
Case ListItemType.Fo oter
lc.Text = "<I>Footer</I>"
container.Contr ols.Add(lc)
End Select
End Sub
Sub DataGridTemplat e_DataBinding(B yVal sender As Object, ByVal
e As System.EventArg s)
Dim container As DataGridItem
Dim lc As Literal
lc = CType(sender, Literal)
container = CType(lc.Naming Container, DataGridItem)
If Not IsDBNull(DataBi nder.Eval(conta iner.DataItem,
lc.Text)) Then
lc.Text = DataBinder.Eval (container.Data Item,
lc.Text)
End If
End Sub
End Class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
mstrOption = Request.QuerySt ring("Option")
BuildHeaderTabl e()
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 "DataElemen ts"
dsData = objProj.LoadDat aElements()
End Select
'Set datagrid properties
dGrid.BorderWid th = Unit.Pixel(2)
dGrid.CellPaddi ng = 10
dGrid.GridLines = GridLines.Both
dGrid.BorderCol or = ConvertHexToSys temColor("0099c c")
dGrid.ShowHeade r = True
dGrid.AutoGener ateColumns = False
dGrid.SelectedI temStyle.BackCo lor = Color.Yellow
dGrid.Alternati ngItemStyle.Bac kColor =
ConvertHexToSys temColor("0099c c")
'Allow edit
Dim editCol As New EditCommandColu mn
editCol.EditTex t = GetImageText("E dit")
editCol.CancelT ext = GetImageText("C ancel")
editCol.UpdateT ext = GetImageText("U pdate")
editCol.ItemSty le.Wrap = False
editCol.ItemSty le.Width = Unit.Pixel(100)
dGrid.Columns.A dd(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.Hea derTemplate = New
DataGridTemplat e(ListItemType. Header, dCol.ColumnName )
colTemplate.Ite mTemplate = New
DataGridTemplat e(ListItemType. Item, dCol.ColumnName )
colTemplate.Edi tItemTemplate = New
DataGridTemplat e(ListItemType. EditItem, dCol.ColumnName )
'colTemplate.Fo oterTemplate = New
DataGridTemplat e(ListItemType. Footer, dCol.ColumnName )
dGrid.Columns.A dd(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 "DataElemen ts"
dsData = objProj.LoadDat aElements()
End Select
dGrid.DataSourc e = dsData
dGrid.DataKeyFi eld = "DataElemen tID"
dGrid.DataBind( )
End Sub
Sub dGrid_Edit(ByVa l Sender As Object, ByVal E As
DataGridCommand EventArgs) Handles dGrid.EditComma nd
dGrid.EditItemI ndex = CInt(E.Item.Ite mIndex)
BindGrid()
End Sub
article on this and I'm so close. Article:
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 "DataGridTempla te_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 DataGridTemplat e
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(B yVal container As Control) _
Implements ITemplate.Insta ntiateIn
Dim lc As New Literal
Select Case templateType
Case ListItemType.He ader
lc.Text = "<B>" & columnName & "</B>"
container.Contr ols.Add(lc)
Case ListItemType.It em
lc.Text = columnName
AddHandler lc.DataBinding, AddressOf
DataGridTemplat e_DataBinding
container.Contr ols.Add(lc)
Case ListItemType.Ed itItem
Dim tb As New TextBox
tb.Text = ""
tb.ID = columnName
container.Contr ols.Add(tb)
Case ListItemType.Fo oter
lc.Text = "<I>Footer</I>"
container.Contr ols.Add(lc)
End Select
End Sub
Sub DataGridTemplat e_DataBinding(B yVal sender As Object, ByVal
e As System.EventArg s)
Dim container As DataGridItem
Dim lc As Literal
lc = CType(sender, Literal)
container = CType(lc.Naming Container, DataGridItem)
If Not IsDBNull(DataBi nder.Eval(conta iner.DataItem,
lc.Text)) Then
lc.Text = DataBinder.Eval (container.Data Item,
lc.Text)
End If
End Sub
End Class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e
As System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
mstrOption = Request.QuerySt ring("Option")
BuildHeaderTabl e()
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 "DataElemen ts"
dsData = objProj.LoadDat aElements()
End Select
'Set datagrid properties
dGrid.BorderWid th = Unit.Pixel(2)
dGrid.CellPaddi ng = 10
dGrid.GridLines = GridLines.Both
dGrid.BorderCol or = ConvertHexToSys temColor("0099c c")
dGrid.ShowHeade r = True
dGrid.AutoGener ateColumns = False
dGrid.SelectedI temStyle.BackCo lor = Color.Yellow
dGrid.Alternati ngItemStyle.Bac kColor =
ConvertHexToSys temColor("0099c c")
'Allow edit
Dim editCol As New EditCommandColu mn
editCol.EditTex t = GetImageText("E dit")
editCol.CancelT ext = GetImageText("C ancel")
editCol.UpdateT ext = GetImageText("U pdate")
editCol.ItemSty le.Wrap = False
editCol.ItemSty le.Width = Unit.Pixel(100)
dGrid.Columns.A dd(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.Hea derTemplate = New
DataGridTemplat e(ListItemType. Header, dCol.ColumnName )
colTemplate.Ite mTemplate = New
DataGridTemplat e(ListItemType. Item, dCol.ColumnName )
colTemplate.Edi tItemTemplate = New
DataGridTemplat e(ListItemType. EditItem, dCol.ColumnName )
'colTemplate.Fo oterTemplate = New
DataGridTemplat e(ListItemType. Footer, dCol.ColumnName )
dGrid.Columns.A dd(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 "DataElemen ts"
dsData = objProj.LoadDat aElements()
End Select
dGrid.DataSourc e = dsData
dGrid.DataKeyFi eld = "DataElemen tID"
dGrid.DataBind( )
End Sub
Sub dGrid_Edit(ByVa l Sender As Object, ByVal E As
DataGridCommand EventArgs) Handles dGrid.EditComma nd
dGrid.EditItemI ndex = CInt(E.Item.Ite mIndex)
BindGrid()
End Sub
Comment