@ Shreekar
Public Class GridViewTemplate
Implements ITemplate
Private templateType As DataControlRowType
Private columnName As String
Sub New(ByVal type As DataControlRowType, ByVal colname As String)
templateType = type
columnName = colname
End Sub
Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements ITemplate.InstantiateIn
Select Case templateType
Case DataControlRowType.Header
Dim lc As New Literal
lc.Text = " " & UCase(columnName) & ""
container.Controls.Add(lc)
Case DataControlRowType.DataRow
Dim firstName As New TextBox
firstName = New TextBox
Dim lastName As New Label
firstName.ID = "txtFName"
Dim spacer = New Literal
spacer.Text = "--"
AddHandler firstName.DataBinding, AddressOf FirstName_DataBinding
AddHandler lastName.DataBinding, AddressOf LastName_DataBinding
lastName.Visible = False
container.Controls.Add(firstName)
container.Controls.Add(spacer)
container.Controls.Add(lastName)
Case Else
End Select
End Sub
Private Sub FirstName_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim l As TextBox = CType(sender, TextBox)
Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)
l.Text = DataBinder.Eval(row.DataItem, "Doctor_Name").ToString()
End Sub
Private Sub LastName_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
Dim l As Label = CType(sender, Label)
Dim row As GridViewRow = CType(l.NamingContainer, GridViewRow)
l.Text = DataBinder.Eval(row.DataItem, "SlNo").ToString()
End Sub
End Class
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Label1.Text = "Entered"
Dim dt As DataTable
Dim dr As DataRow
dt = New DataTable()
dt.Columns.Add(New DataColumn("Doctor_Name"))
dt.Columns.Add(New DataColumn("SlNo"))
dr = dt.NewRow()
dr("Doctor_Name") = "Val1"
dr("Slno") = "1001"
dt.Rows.Add(dr)
Label1.Text = "1 add"
dr = dt.NewRow()
dr("Doctor_Name") = "Val2"
dr("Slno") = "1002"
dt.Rows.Add(dr)
Label1.Text = "2 add"
dr = dt.NewRow()
dr("Doctor_Name") = "Val3"
dr("Slno") = "1003"
dt.Rows.Add(dr)
dr = dt.NewRow()
dr("Doctor_Name") = "val4"
dr("Slno") = "1004"
dt.Rows.Add(dr)
Label1.Text = "4 add"
Dim customField As New TemplateField
Dim CustomField1 As New TemplateField
customField.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "Author Name")
customField.HeaderTemplate = New GridViewTemplate(DataControlRowType.Header, "Employees")
AuthorsGridView.Columns.Add(customField)
AuthorsGridView.DataSource = dt
Label1.Text = " DataSource Set"
AuthorsGridView.DataBind()
ViewState("table") = dt
Label1.Text = "Binded"
For i = 0 To AuthorsGridView.Rows.Count - 1
Dim txt As TextBox = AuthorsGridView.Rows(i).Cells(0).FindControl("txtFName")
Label1.Text = txt.Text
If txt.Text = "VAl2" Then
txt.Font.Bold = True
Else
txt.Font.Italic = True
End If
Next
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim dt As DataTable
dt = ViewState("table")
AuthorsGridView.DataSource = dt
AuthorsGridView.DataBind()
Label1.Text = AuthorsGridView.Rows.Count
For i = 0 To AuthorsGridView.Rows.Count - 1
Dim txt As TextBox = AuthorsGridView.Rows(i).Cells(0).FindControl("txtFName")
Label1.Text = txt.Text
If txt.Text = "Val2" Then
txt.Font.Bold = True
Else
txt.Font.Italic = True
End If
Next
End Sub