C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Dynamic creation of datagrid columns in Datagrid using a vb template


Posted Date: 17 Jun 2008    Resource Type: Code Snippets    Category: DataGridView
Author: komaladeviMember Level: Gold    
Rating: Points: 10



Here is the Code to create the dynamic column in a datagrid .....

Here in this i have created the checkbox column with header , footer , edit and item template


with this vb file we can call in our web page and create the columns



Imports Microsoft.VisualBasic
Imports System
Imports System.Web.UI.WebControls
Imports System.Web
Imports System.Web.UI
Imports System.Collections
Public Class DataGridTempla
Implements ITemplate
Private templateType As ListItemType
Private columnName As String

Public Sub New(ByVal type As ListItemType, ByVal colname As String)
templateType = type
columnName = colname
End Sub

'Public Sub InstantiateIn(ByVal container As System.Web.UI.Control)

' Dim lc As New Literal()
' Dim chkb As New CheckBox()
' 'LinkButton lb = new LinkButton();
' Select Case templateType
' Case ListItemType.Header


' lc.Text = "" + columnName + ""

' 'chkb.Text = "Select";
' 'lb.CommandName = "EditButton";
' container.Controls.Add(chkb)
' container.Controls.Add(lc)

' Exit Select
' Case ListItemType.Item


' 'lc.Text=columnName;
' 'lc.Text = "Select " + columnName;
' 'chkb.Text = "Select";

' chkb.ID = columnName
' container.Controls.Add(chkb)
' container.Controls.Add(lc)

' Exit Select
' Case ListItemType.EditItem


' Dim tb As New TextBox()

' tb.Text = ""

' container.Controls.Add(tb)

' Exit Select
' Case ListItemType.Footer


' lc.Text = "" + columnName + ""

' container.Controls.Add(lc)

' Exit Select



' End Select

'End Sub

Public Sub InstantiateIn1(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Dim lc As New Literal()
Dim chkb As New CheckBox()
'LinkButton lb = new LinkButton();
Select Case templateType
Case ListItemType.Header
lc.Text = "" + columnName + ""
'chkb.Text = "Select";
'lb.CommandName = "EditButton";
container.Controls.Add(chkb)
container.Controls.Add(lc)
Exit Select
Case ListItemType.Item
'lc.Text=columnName;
'lc.Text = "Select " + columnName;
'chkb.Text = "Select";
chkb.ID = columnName
container.Controls.Add(chkb)
container.Controls.Add(lc)
Exit Select
Case ListItemType.EditItem
Dim tb As New TextBox()
tb.Text = ""
container.Controls.Add(tb)
Exit Select
Case ListItemType.Footer
lc.Text = "" + columnName + ""
container.Controls.Add(lc)
Exit Select
End Select
End Sub
End Class










steps to use this ::


create one APPCode class file and copy the above content and create the instance of that code like below in our class and .net application

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Partial Public Class Dynamic_Data_Grid
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Session("DG4_ShoppingCart") Is Nothing Then
Cart = New DataTable()
Cart.Columns.Add(New DataColumn("Item", GetType(String)))
Cart.Columns.Add(New DataColumn("Price", GetType(String)))
Session("DG4_ShoppingCart") = Cart
Else
Cart = DirectCast(Session("DG4_ShoppingCart"), DataTable)
End If
CartView = New DataView(Cart)
ShoppingCart.DataSource = CartView
ShoppingCart.DataBind()

If Not IsPostBack Then
ItemsGrid.DataSource = CreateDataSource()
ItemsGrid.DataBind()
End If

End Sub

Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
CreateDataGridColumn()
InitializeComponent()
MyBase.OnInit(e)
End Sub
Private Sub InitializeComponent()
AddHandler Me.btnSubmit.Click, AddressOf btnSubmit_Click
AddHandler Me.Load, AddressOf Page_Load

End Sub

Public Sub CreateDataGridColumn()
Dim tc1 As New TemplateColumn()
tc1.HeaderTemplate = New DataGridTempla(ListItemType.Header, "Select1")
tc1.ItemTemplate = New DataGridTempla(ListItemType.Item, "Select1")
tc1.EditItemTemplate = New DataGridTempla(ListItemType.EditItem, "")
tc1.FooterTemplate = New DataGridTempla(ListItemType.Footer, "")
ItemsGrid.Columns.Add(tc1)

Dim tc2 As New TemplateColumn()
tc2.HeaderTemplate = New DataGridTempla(ListItemType.Header, "Select2")
tc2.ItemTemplate = New DataGridTempla(ListItemType.Item, "Select2")
ItemsGrid.Columns.Add(tc2)

Dim NumberColumn As New BoundColumn()
NumberColumn.HeaderText = "Item Number"
NumberColumn.DataField = "IntegerValue"
ItemsGrid.Columns.AddAt(2, NumberColumn)

End Sub

Private Cart As DataTable
Private CartView As DataView

Public Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
For i As Integer = 0 To 10
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = 5.0 * (i + 1)

dt.Rows.Add(dr)
Next
Dim dv As New DataView(dt)
Return dv
End Function

Public Sub Grid_CartCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Dim dr As DataRow = Cart.NewRow()
Dim itemCell As TableCell = e.Item.Cells(2)
Dim priceCell As TableCell = e.Item.Cells(3)
Dim item As String = itemCell.Text
Dim price As String = priceCell.Text

If DirectCast(e.CommandSource, Button).CommandName = "AddToCart" Then
dr(0) = item
dr(1) = price
Cart.Rows.Add(dr)
Else
'Remove from Cart.
CartView.RowFilter = "Item='" + item + "'"
If CartView.Count > 0 Then
CartView.Delete(0)
End If
CartView.RowFilter = ""
End If
ShoppingCart.DataBind()



End Sub

Private Sub DetermineSelection(ByVal item As DataGridItem, ByRef count As Integer)
Dim selection As CheckBox = DirectCast(item.FindControl("Select2"), CheckBox)
If selection IsNot Nothing Then
If selection.Checked Then
Response.Write("The Item is '" + item.Cells(3).Text + "' from Select 2 Column" + "
")
count += 1
End If
End If
Dim Select1 As CheckBox = DirectCast(item.FindControl("Select1"), CheckBox)
If Select1 IsNot Nothing Then
If Select1.Checked Then
Response.Write("The Item is '" + item.Cells(3).Text + "' from Select 1 Column" + "
")
count += 1
End If
End If
End Sub


Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim count As Integer = 0
Response.Write("The Items you have Selected is/are :
")
For Each item As DataGridItem In ItemsGrid.Items
DetermineSelection(item, count)
Next
If count = 0 Then
Response.Write("No items selected")
End If
End Sub
End Class




you people are always invited with doubts for this code snippet at komalinaughty@gmail.com




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: checkbox in a Datagrid2 which is inside the Datagrid1
Previous Resource: Add Checkbox to ListView in WPF
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use