To add a row to a datagrid header dynamically use following code. Here we just adding new cells to the the datagrid at header ItemDataBound Event.
#VB.Net code:
Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal ev As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid.ItemDataBound ' Add a second header on a datagrid
If ev.Item.ItemType = ListItemType.Header Then ' Get the collection of cells from the datagrid
Dim totCells As TableCellCollection
' Get the collection of existing cells so we can get a count
totCells = ev.Item.Cells
' Create a new datagrid header object
Dim dgHeader As New DataGridItem(0, 0, ListItemType.Header) ' Add the created cell to the header
Dim vblCount As Integer
Dim vblCounter As Integer
vblCount = totCells.Count
vblCounter = 0
While vblCount > 0
'Creation of a new cell for datagrid
Dim CreatedCell As New TableCell
'To skip first cell of our grid
If vblCounter > 1 Then
CreatedCell.Text = "Text for New Cell"
End If
dgHeader.Cells.Add(CreatedCell)
vblCounter = vblCounter + 1
vblCount = vblCount - 1
End While 'While Closes here
dgHeader.Visible = True ' Add the header to the datagrid
DataGrid.Controls(0).Controls.Add(dgHeader)
End If ' If ends here
End Sub ' ItemDataBound Ends here

Showing posts with label Add. Show all posts
Showing posts with label Add. Show all posts
Friday, June 13, 2008
Wednesday, April 30, 2008
Adding Empty Row to a Data Grid
How to add empty row in a datagrid.
* Create and add an empty Row for a Data Table.
* Assign the Datatable as dataSource for the datagrid.
* Bind the datagrid.
#VB.NET
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt as new DataTable
dt.rows.add(dt.NewRow())
DataGrid1.DataSource = dt
DataGrid1.DataBind()
End Sub
If we need just a template of a datagrid at runtime means, just bind an empty DataTable like below
#VB.NET
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt as new DataTable
DataGrid1.DataSource = dt
DataGrid1.DataBind()
End Sub
* Create and add an empty Row for a Data Table.
* Assign the Datatable as dataSource for the datagrid.
* Bind the datagrid.
#VB.NET
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt as new DataTable
dt.rows.add(dt.NewRow())
DataGrid1.DataSource = dt
DataGrid1.DataBind()
End Sub
If we need just a template of a datagrid at runtime means, just bind an empty DataTable like below
#VB.NET
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dt as new DataTable
DataGrid1.DataSource = dt
DataGrid1.DataBind()
End Sub
Subscribe to:
Posts (Atom)