473,419 Members | 4,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

Programmatically Created DataGrid Template Columns

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
2 4518
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: cmc | last post by:
Hi, I have a web application that programmatically creates DataGrids. When I create the DataGrid I set the EditItemStyle.CssClass = "DataGridStyleEdit" - the background of the row and the ...
4
by: | last post by:
I have a datagrid with a template column that has a hyperlink and a label. The hyperlink text is bound to Title from my dataset and the label text is bound to Author in the dataset. The grid...
1
by: André Almeida Maldonado | last post by:
Hy Guys.. I have a datagrid with template columns. The datagrid have a datatable datasource. But when I try to bind data, I receive this error: DataBinder.Eval:...
1
by: cgia | last post by:
So far I have used an hyperlink column in a datagrid to transfer to another aspx form, using one querystring parameter. Now I want to keep using that hyperlink, but would like to send 6...
0
by: R Corona | last post by:
I am creating a datagrid that has four columns. I am creating the first, an edit column and the last in the html of the page. The middle two are being added in the code behind. One is a readonly...
0
by: Davey P | last post by:
I have a datagrid and am populating it with template columns dynamically at runtime. I have created a class that inherits ITemplate and populates the cells with a Drop Down List in the...
2
by: Kel Good | last post by:
I have a shopping cart datagrid that is filled only the first time the page is accessed, like so: If Page.IsPostBack = False Then RetrieveShoppingCart() End If The user can then change the...
0
by: TB | last post by:
Hi All For reasons unknown to me, the Page.IsPostBack is always false when I click a link in a hyperlink column of a datagrid which is created programmatically. My code in a page called...
0
by: jtencate | last post by:
I am creating a gridview programmatically and am using a Template for one column. The template is derived from a custom class that inherits ITemplate. There are two buttons in the template The...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.