-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: viku
Message 7 in Discussion

Hi Rupreet,   I am sending u the code, This whole code is of the class,I am creating 
the instance of this class in the aspx codebehind file and passing the Table name as 
parameter. for that table i have to get the Datagrid with the columns and rows for 
editing,updating and deleting. ****************************** Imports 
System.ComponentModel
Imports System.Web.UI
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Public Class DataGridClassTemp
    Inherits System.Web.UI.WebControls.DataGrid
    Private m_ConnectionString As String
    Private m_strTableName As String
    Private DS As DataSet
    Protected WithEvents myDeleteButton As LinkButton
    Protected WithEvents myAddButton As Button     Public Property ConnectionString() 
As String
        Get
            Return m_ConnectionString
        End Get
        Set(ByVal Value As String)
            m_ConnectionString = Value
        End Set
    End Property
    Public Property TableName() As String
        Get
            Return m_strTableName
        End Get
        Set(ByVal Value As String)
            m_strTableName = Value
        End Set
    End Property
    Public Sub New(ByVal ChildTableName As String, ByVal ConnectionStr As String)
        m_strTableName = ChildTableName
        m_ConnectionString = ConnectionStr
    End Sub
    Public Sub BindDataGrid()
        Dim Conn As SqlConnection = New SqlConnection(m_ConnectionString)
        Dim DAdapt As SqlDataAdapter
        Dim strSql As String
        Dim cmd As SqlCommand         MyBase.ID = m_strTableName.ToString()
        MyBase.AllowPaging = True
        MyBase.AutoGenerateColumns = False
        MyBase.ShowFooter = True
        MyBase.ShowHeader = True
        MyBase.Font.Size = System.Web.UI.WebControls.FontUnit.XSmall
        MyBase.Font.Name() = "Arial"
        MyBase.CellPadding = 3
        MyBase.Width.Pixel(800)
        MyBase.Height.Pixel(200)
        MyBase.BackColor = System.Drawing.Color.White
        MyBase.BorderWidth = System.Web.UI.WebControls.Unit.Pixel(1)
        MyBase.BorderStyle = BorderStyle.None
        MyBase.BorderColor = System.Drawing.Color.Black
        MyBase.PagerStyle.Mode = PagerMode.NumericPages
        MyBase.SelectedItemStyle.Font.Bold = True
        MyBase.Columns.Clear()         AddHandler MyBase.ItemCreated, AddressOf 
DataGridClassTemp_ItemCreated
        AddHandler MyBase.ItemDataBound, AddressOf DataGridClassTemp_ItemDataBound     
    AddHandler MyBase.EditCommand, AddressOf DataGridClassTemp_EditCommand
        AddHandler MyBase.UpdateCommand, AddressOf DataGridClassTemp_UpdateCommand
        'AddHandler MyBase.CancelCommand, AddressOf DataGrid1_CancelCommand         DS 
= GetDataSet()
        Dim strTableName As String = m_strTableName
        Dim byCtr As Integer
        'strSql = "Select FieldId, FieldName, CtrlId, DataTypeId, Size"
        'strSql += " From tbl_FM_FMChild_Meta_Tr"
        'strSql += " Where FormFieldKey = " & CInt(strTableName)
        'strSql += " Order By FieldId"
        'DAdapt = New SqlDataAdapter(strSql, Conn)
        'DAdapt.Fill(DS, "tbl_FM_FMChild_Meta_Tr")         'Conn.Close()
        'If DS.Tables("tbl_FM_FMChild_Meta_Tr").Rows.Count > 0 Then
        'For byCtr = 0 To DS.Tables("tbl_FM_FMChild_Meta_Tr").Rows.Count - 1
        For byCtr = 0 To DS.Tables(m_strTableName).Columns.Count - 1
            Dim objTC As New TemplateColumn()
            objTC.ItemStyle.Wrap = True
            'objTC.HeaderTemplate = New DataGridTemplate(ListItemType.Header, 
DS.Tables("tbl_FM_FMChild_Meta_Tr").Rows(byCtr).Item("FieldName").ToString())
            objTC.HeaderTemplate = New DataGridTemplate(ListItemType.Header, 
DS.Tables(m_strTableName).Columns(byCtr).ColumnName)
            objTC.ItemTemplate = New DataGridTemplate(ListItemType.Item, 
DS.Tables(m_strTableName).Columns(byCtr).ColumnName.ToString())
            objTC.EditItemTemplate = New DataGridTemplate(ListItemType.EditItem, 
DS.Tables(m_strTableName).Columns(byCtr).ColumnName.ToString())
            If byCtr = 0 Then
                objTC.FooterTemplate = New DataGridTemplate(ListItemType.Footer, 
m_strTableName.ToString())
            End If
            MyBase.Columns.Add(objTC)
        Next         Dim objEcol As New EditCommandColumn()
        objEcol.ButtonType = ButtonColumnType.LinkButton
        objEcol.EditText = "Edit"
        objEcol.UpdateText = "Update"
        objEcol.CancelText = "Cancel"
        objEcol.HeaderText = "Edit ?"
        objEcol.HeaderStyle.Font.Bold = True
        MyBase.Columns.Add(objEcol)         Dim ObjItemTc As New TemplateColumn()
        ObjItemTc = New TemplateColumn()
        ObjItemTc.HeaderTemplate = New DataGridTemplate(ListItemType.Header, "Delete 
?")
        ObjItemTc.ItemTemplate = New DataGridTemplate(ListItemType.Item, 
m_strTableName.ToString(), DataGridTemplate.ItemControlType.Button)
        MyBase.Columns.Add(ObjItemTc) 
        MyBase.DataSource = DS.Tables(m_strTableName)
        MyBase.DataBind()
        'End If
    End Sub     Public Function GetDataSet() As DataSet
        Dim Conn As New SqlConnection(m_ConnectionString)
        Dim Ada As SqlDataAdapter
        Dim StrSql As String
        Dim Ds As New DataSet("FormField")
        Dim Cmd As SqlCommand
        'Dim strParentTblName() As String = m_strTableName.Split("_")
        Conn.Open()
        StrSql = "Select * from " & m_strTableName
        Ada = New SqlDataAdapter(StrSql, Conn)
        Ada.Fill(Ds, m_strTableName.ToString())
        Return Ds.Tables(m_strTableName).DataSet
    End Function       Private Sub DataGridClassTemp_ItemDataBound(ByVal sender As 
Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles 
MyBase.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = 
ListItemType.AlternatingItem Then
            Dim drv As DataRowView = CType(e.Item.DataItem, DataRowView)
            Dim byCtr As Byte
            For byCtr = 0 To DS.Tables(m_strTableName).Columns.Count - 1
                Dim MyLbl As Label = CType(e.Item.Cells(byCtr).Controls(0), Label)
                MyLbl.Text = 
drv(DS.Tables(m_strTableName).Columns(byCtr).ColumnName.ToString()).ToString()
            Next
        ElseIf e.Item.ItemType = ListItemType.EditItem Then
            Dim Drv As DataRowView = CType(e.Item.DataItem, DataRowView)
            Dim byCtr As Byte
            For byCtr = 0 To DS.Tables(m_strTableName).Columns.Count - 1
                Dim Txt As TextBox = CType(e.Item.FindControl("txt" & 
DS.Tables(m_strTableName).Columns(byCtr).ColumnName.ToString()), TextBox)
                Txt.Text = 
Drv(DS.Tables(m_strTableName).Columns(byCtr).ColumnName.ToString()).ToString()
            Next
        End If
    End Sub
    Protected Sub btnAddRow_Click(ByVal sender As System.Object, ByVal e As 
System.EventArgs)
        'Dim Conn As New SqlConnection(m_ConnectionString)
        'Dim StrSql As String
        'Conn.Open()
        'StrSql = "Insert Into [" & MyClass.ID & "] (TransactionId)"
        'StrSql += " Values(" & m_TransactionId & ")"
        'Dim cmd As New SqlCommand(StrSql, Conn)
        'cmd.ExecuteNonQuery()
        'Conn.Close()
        'BindGrid()
    End Sub       Private Sub DataGridClassTemp_EditCommand(ByVal source As Object, 
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles 
MyBase.EditCommand
        MyBase.EditItemIndex = CInt(e.Item.ItemIndex)
        DS = GetDataSet()
        MyBase.DataSource = DS.Tables(m_strTableName)
        MyBase.DataBind()
    End Sub     Private Sub DataGridClassTemp_UpdateCommand(ByVal source As Object, 
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles 
MyBase.UpdateCommand
        MyBase.EditItemIndex = -1
        BindDataGrid()
        'DataGrid()
    End Sub 
    'Private Sub DataGridClassTemp_ItemCommand(ByVal source As Object, ByVal e As 
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles MyBase.ItemCommand
    '    Dim cmdname As String
    '    cmdname = e.CommandName     '    MyBase.EditItemIndex = -1
    '    BindDataGrid()
    'End Sub     Private Sub DataGridClassTemp_ItemCreated(ByVal sender As Object, 
ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles MyBase.ItemCreated
        Select Case e.Item.ItemType
            Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.EditItem
                myDeleteButton = e.Item.FindControl("lnkbtnDelete")
                myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure 
you want to delete this Record?');")
                AddHandler myDeleteButton.Click, AddressOf 
DataGridClassTemp_DeleteCommand
            Case ListItemType.Footer
                Dim btnAddRecord As String = "btn" & m_strTableName
                myAddButton = e.Item.FindControl(btnAddRecord)
                If IsNothing(myAddButton) = False Then
                    AddHandler myAddButton.Click, AddressOf btnAddRow_Click
                End If
        End Select
    End Sub     Private Sub DataGridClassTemp_DeleteCommand(ByVal sender As Object, 
ByVal e As System.EventArgs)
        '    'Dim Conn As New SqlConnection(m_ConnectionString)
        '    'Dim StrSql As String
        '    'Conn.Open()
        '    ''StrSql = "Delete From [" & MyClass.ID & "]"
        '    ''StrSql += " Where TransactionId = " & m_TransactionId
        '    'StrSql = "Delete From [" & MyClass.ID & "]"
        '    'StrSql += " Where job_id = " &          '    'Dim cmd As New 
SqlCommand(StrSql, Conn)
        '    'cmd.ExecuteNonQuery()
        '    'Conn.Close()
        '    'BindGrid()
    End Sub
End Class


-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to