Public Class WebForm10
Inherits System.Web.UI.Page
Dim dtHead As DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub btn_search_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_search.Click
If GridView1.Rows.Count > 0 Then
GridView1.Columns.Clear()
End If
AddTemplate()
addDatasource()
End Sub
Sub AddTemplate()
Dim objTransaction As New transaction
Dim resultDtHead As New DataTable
resultDtHead = objTransaction.selectAllHeadGridview(dropCompany.SelectedValue, dropItem.SelectedValue)
Session("bound_head") = resultDtHead
Dim j As Integer
For j = 0 To resultDtHead.Rows.Count - 1
Dim dCol As New TemplateField()
dCol.HeaderText = resultDtHead.Rows(j)("cost_name")
dCol.ItemTemplate = New GridviewTemplate1(ListItemType.Item, resultDtHead.Rows(j)("cost_name"), j)
GridView1.Columns.Add(dCol)
Next
End Sub
Sub addDatasource()
Dim objTransaction As New transaction
Dim resultDtHead As New DataTable
Dim resulthead As New DataTable
Dim str_head_cost As String = ""
resulthead = objTransaction.selectHeadGridview(dropCompany.SelectedValue, dropItem.SelectedValue)
Dim k As Integer
For k = 0 To resulthead.Rows.Count - 1
If str_head_cost.Trim = "" Then
str_head_cost = resulthead.Rows(0)("cost_name")
Else
str_head_cost = str_head_cost & "," & resulthead.Rows(k)("cost_name")
End If
Next
Dim resultDt As New DataTable
resultDt = objTransaction.selectDetailForWaitApp(dropCompany.SelectedValue, dropItem.SelectedValue, str_head_cost)
Session("bound_data") = resultDtHead
GridView1.DataSource = resultDt
GridView1.DataBind()
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
dtHead = Session("bound_head")
Dim i As Integer
For i = 0 To dtHead.Rows.Count - 1
If i <> 0 Then
Dim lblHead As Label = CType(e.Row.FindControl(dtHead.Rows(i)("cost_name")), Label)
If Not IsNothing(lblHead) Then
If e.Row.DataItem(dtHead.Rows(i)("cost_name")).ToString.Trim.Length > 0 Then
lblHead.Text = e.Row.DataItem(dtHead.Rows(i)("cost_name"))
Else
lblHead.Text = "-"
End If
End If
End If
Next
End Sub
Protected Sub Button2_Click1(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
End Sub
End Class
Class สร้าง Template Code (VB.NET)
Public Class GridviewTemplate1
Implements ITemplate
Dim templateType As ListItemType
Dim str_id As String
Dim str_no As Integer
Sub New(ByVal type As ListItemType, ByVal str_cost As String, ByVal no As Integer)
templateType = type
str_id = str_cost
str_no = no
End Sub
Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) _
Implements System.Web.UI.ITemplate.InstantiateIn
If str_no = 0 Then
Dim cb As New CheckBox
'cb.AutoPostBack = True
cb.ID = "chkId"
container.Controls.Add(cb)
Else
Select Case (templateType)
Case ListItemType.Header
Dim txt As New TextBox
txt.Text = "Header"
container.Controls.Add(txt)
Case ListItemType.Item
Dim lbl As New Label
lbl.ID = str_id
container.Controls.Add(lbl)
Case ListItemType.Footer
Dim txt As New TextBox
container.Controls.Add(txt)
End Select
End If
}
End Sub
End Class