Thursday, May 27, 2010


Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
End If
End Sub

Private Sub PopulateGridView()
Dim colObjects As New Generic.List(Of MyObject)
For intCount As Integer = 1 To 20
Dim objNew As New MyObject(intCount, "Object number " + intCount.ToString())
Me.GridView1.DataSource = colObjects
End Sub

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
Select Case e.CommandName
Case "DoAction"
Me.litMessage.Text = "DoAction called for Id " + e.CommandArgument
End Select
End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

' Make sure this isn't the header
If e.Row.DataItem IsNot Nothing Then
Dim intCurrentId As Integer = CType(e.Row.DataItem, MyObject).Id
If intCurrentId Mod 10 = 0 Then
' Add a row before this record
Dim tblGrid As Table = CType(Me.GridView1.Controls(0), Table)
' Get the rowindex of the current row
Dim intIndex As Integer = tblGrid.Rows.GetRowIndex(e.Row)
' Create a new row to hold our subheading
Dim gvrSubHeading As New GridViewRow(intIndex, intIndex, DataControlRowType.Separator, DataControlRowState.Normal)
' Create a table cell for the row
Dim cellHeader As New TableCell()
' Set the colspan of the cell to the width of the table
cellHeader.ColumnSpan = Me.GridView1.Columns.Count
' Set the text
cellHeader.Text = "Subheading here"
' Add the cell to the row
' Add the row to the table
tblGrid.Controls.AddAt(intIndex, gvrSubHeading)
End If
End If

End Sub
End Class

Public Class MyObject

Private _intId As Integer
Private _strName As String

Public ReadOnly Property Id() As Integer
Return Me._intId
End Get
End Property

Public Property Name() As String
Return Me._strName
End Get
Set(ByVal value As String)
Me._strName = value
End Set
End Property

Public Sub New(ByVal pId As Integer, ByVal pName As String)
Me._intId = pId
Me._strName = pName
End Sub

End Class

No comments:

Post a Comment