hello
I want to add GridTemplateColumn and Textbox as itemtemplate programatically.
I used the below code but it just generate the GridTemplate columns with blank rows without textbox as shown in attached image
Please help me to solve this problem.
Protected
Sub
btnFurnaceAdd_Click(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnFurnaceAdd.Click
num = gdFurnace.Items.Count + 1
Dim
dt
As
New
DataTable()
gdFurnace.DataSource = addBlankLines(dt, num)
gdFurnace.Rebind()
For
i = 0
To
8
Dim
col
As
New
GridTemplateColumn
col.HeaderText =
"Column"
+ i.ToString
col.UniqueName = i.ToString
col.ItemTemplate =
New
MyTemplate(col.UniqueName)
col.HeaderStyle.Font.Bold =
True
gdFurnace.MasterTableView.Columns.Add(col)
Next
End
Sub
Private Function addBlankLines(ByVal dt As DataTable, ByVal num As Integer) As DataTable
Dim count As Integer = 0
If num <> 0 Then
Dim drBlank As DataRow = Nothing
For i As Integer = 0 To num - 1
drBlank = dt.NewRow()
dt.Rows.Add(drBlank)
Next
End If
Return dt
End Function
Private Class MyTemplate
Implements ITemplate
Protected lControl As LiteralControl
Protected validatorTextBox As RequiredFieldValidator
Protected searchGoogle As HyperLink
Protected textBox As TextBox
Protected boolValue As CheckBox
Private colname As String
Public Sub New(ByVal cName As String)
colname = cName
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
lControl = New LiteralControl()
lControl.ID = "lControl"
AddHandler lControl.DataBinding, AddressOf lControl_DataBinding
textBox = New TextBox()
textBox.ID = "templateColumnTextBox"
validatorTextBox = New RequiredFieldValidator()
validatorTextBox.ControlToValidate = "templateColumnTextBox"
validatorTextBox.ErrorMessage = "*"
searchGoogle = New HyperLink()
searchGoogle.ID = "searchGoogle"
AddHandler searchGoogle.DataBinding, _
AddressOf searchGoogle_DataBinding
boolValue = New CheckBox()
boolValue.ID = "boolValue"
AddHandler boolValue.DataBinding, _
AddressOf boolValue_DataBinding
boolValue.Enabled = False
Dim table As New Table()
Dim row1 As New TableRow()
Dim row2 As New TableRow()
Dim cell11 As New TableCell()
Dim cell12 As New TableCell()
Dim cell21 As New TableCell()
Dim cell22 As New TableCell()
row1.Cells.Add(cell11)
row1.Cells.Add(cell12)
row2.Cells.Add(cell21)
row2.Cells.Add(cell22)
table.Rows.Add(row1)
table.Rows.Add(row2)
cell11.Text = colname + ": "
cell12.Controls.Add(lControl)
cell21.Text = "Search Google for: "
cell22.Controls.Add(searchGoogle)
container.Controls.Add(textBox)
container.Controls.Add(validatorTextBox)
container.Controls.Add(table)
container.Controls.Add(New LiteralControl("<br />"))
container.Controls.Add(boolValue)
End Sub
Sub boolValue_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim cBox As CheckBox = DirectCast(sender, CheckBox)
Dim container As GridDataItem = DirectCast(cBox.NamingContainer, GridDataItem)
cBox.Checked = DirectCast((DirectCast(container.DataItem, DataRowView))("Bool"), Boolean)
End Sub
Sub searchGoogle_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim link As HyperLink = DirectCast(sender, HyperLink)
Dim container As GridDataItem = DirectCast(link.NamingContainer, GridDataItem)
link.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString()
link.NavigateUrl = "http://www.google.com/search?hl=en&q=" + _
(DirectCast(container.DataItem, DataRowView))("ContactName").ToString() + "&btnG=Google+Search"
End Sub
Public Sub lControl_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim l As LiteralControl = DirectCast(sender, LiteralControl)
Dim container As GridDataItem = DirectCast(l.NamingContainer, GridDataItem)
l.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString() + "<br />"
End Sub
End Class