Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 22nd, 2004, 07:26 AM
Registered User
 
Join Date: Dec 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamic Datagrid

I am buliding one datagrid using xml. From Xml i am getting all the details for the generation of Datagrid. the problem is i am able to call the event for edit but on the particular edititemindex none of the events r working-Update,Cancel. i am Struggling to complete this form .plz Give me a solution. This Form is incomplete.



   Case "DATAGRID"
                Dim objDb As New DBOperations()
                Dim DataGrid1 As New DataGrid()
                DataGrid1.EditItemStyle.CssClass = "eis"
                DataGrid1.AlternatingItemStyle.CssClass = "ais"
                DataGrid1.ItemStyle.CssClass = "is"
                DataGrid1.HeaderStyle.CssClass = "hs"
                DataGrid1.FooterStyle.CssClass = "fs"
                DataGrid1.ShowFooter = True
                DataGrid1.ShowHeader = True
                DataGrid1.ID = "DataGrid1"
                If hdnString.Value <> "" Then
                    DataGrid1.EnableViewState = True
                End If
                DataGrid1.AutoGenerateColumns = False
                DataGrid1.Width = Unit.Percentage(100)
                strCaption = CStr(attrColl("MemberName").Value).Split(",")
                strControls = CStr(attrColl("MemberControls").Value).Split(",")
                If attrColl("Edit").Value = "True" Then
                    Dim tc1 As New TemplateColumn()
                    tc1.HeaderTemplate = New DataGridTemplateLink(ListItemType.Header, "")
                    tc1.ItemTemplate = New DataGridTemplateLink(ListItemType.Item, strCaption(i))
                    tc1.EditItemTemplate = New DataGridTemplateLink(ListItemType.EditItem, strCaption(i))
                    tc1.FooterTemplate = New DataGridTemplateLink(ListItemType.Footer, strCaption(i))
                    DataGrid1.Columns.Add(tc1)

                    Dim bc As New ButtonColumn()
                    bc.CommandName = "Delete"
                    bc.Text = "Delete"
                    DataGrid1.Columns.Add(bc)
                End If
                For i = 0 To attrColl("DTCount").Value - 1
                    Dim tc1 As New TemplateColumn()
                    tc1.HeaderTemplate = New DataGridTemplate(ListItemType.Header, strCaption(i), strControls(i))
                    tc1.ItemTemplate = New DataGridTemplate(ListItemType.Item, strCaption(i), strControls(i))
                    tc1.EditItemTemplate = New DataGridTemplate(ListItemType.EditItem, strCaption(i), strControls(i))
                    tc1.FooterTemplate = New DataGridTemplate(ListItemType.Footer, strCaption(i), strControls(i))
                    DataGrid1.Columns.Add(tc1)
                Next
                If Not ViewState("EditItemIndex") Is Nothing Then
                    CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = ViewState("EditItemIndex")
                End If

                AddHandler DataGrid1.UpdateCommand, AddressOf UpdateCommand
                AddHandler DataGrid1.ItemCommand, AddressOf ItemCommand
                AddHandler DataGrid1.ItemDataBound, AddressOf DataGrid1_ItemDataBound
                AddHandler DataGrid1.EditCommand, AddressOf EditCommand
                AddHandler DataGrid1.DeleteCommand, AddressOf DeleteCommand
                AddHandler DataGrid1.CancelCommand, AddressOf CancelCommand

                For i = 0 To attrColl("DTCount").Value - 1
                    myDataTable.Columns.Add(strCaption(i))
                Next
                Dim dsEditDetail As New DataTable()
                Dim strRow As DataRow
                Dim tblRow As DataRow
                dsEditDetail = objDb.GetDataTableSP("Usp_master_Detail_select")
                For Each strRow In dsEditDetail.Rows
                    tblRow = myDataTable.NewRow
                    For i = 0 To attrColl("DTCount").Value - 1
                        tblRow(strCaption(i)) = strRow.Item(i)
                    Next
                    myDataTable.Rows.Add(tblRow)
                Next
                If hdnString.Value = "" Then
                    DataGrid1.DataSource = myDataTable
                    DataGrid1.DataBind()
                    Session("myDataTable") = myDataTable
                    hdnString.Value = 1
                Else
                    myDataTable = Session("myDataTable")
                    DataGrid1.DataSource = myDataTable
                    DataGrid1.DataBind()
                    Session("myDataTable") = myDataTable
                End If
                controlcell.Controls.Add(DataGrid1)
                controlcell.ColSpan = 2
                row.Controls.Add(controlcell)
                MainForm.Controls.Add(row)
        End Select







 Private Class DataGridTemplate
        Implements ITemplate
        Dim templateType As ListItemType
        Dim columnName As String
        Dim controlName As String

        Sub New(ByVal type As ListItemType, ByVal ColName As String, ByVal conName As String)
            templateType = type
            columnName = ColName
            controlName = conName
        End Sub

        Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
            Dim lc As New Label()
            Select Case templateType
                Case ListItemType.Header
                    lc.Text = "<B>" & columnName & "</B>"
                    container.Controls.Add(lc)
                Case ListItemType.Item
                    lc.ID = "lbl" & columnName
                    AddHandler lc.DataBinding, AddressOf BindStringColumn
                    container.Controls.Add(lc)
                Case ListItemType.EditItem
                    Dim LabelEdit As New Label()
                    LabelEdit.ID = "lblE" & columnName
                    AddHandler LabelEdit.DataBinding, AddressOf BindStringColumn
                    LabelEdit.Visible = False
                    container.Controls.Add(LabelEdit)
                    If controlName = "TEXTBOX" Then
                        Dim tb As New TextBox()
                        tb.ID = "txtE" & columnName
                        container.Controls.Add(tb)
                        tb.Text = LabelEdit.Text
                    End If
                    If controlName = "RADIOBUTTON" Then
                        Dim rad As New TextBox()
                        rad.Text = ""
                        container.Controls.Add(rad)
                    End If
                    If controlName = "ComboBox" Then
                        Dim ddl As New DropDownList()
                        container.Controls.Add(ddl)
                    End If
                    If controlName = "DateTime" Then
                        Dim tb As New TextBox()
                        tb.Text = ""
                        container.Controls.Add(tb)
                    End If
                    If controlName = "TextArea" Then
                        Dim tb As New TextBox()
                        tb.Text = ""
                        tb.TextMode = TextBoxMode.MultiLine
                        tb.Width = Unit.Pixel(200)
                        tb.Height = Unit.Pixel(65)
                        container.Controls.Add(tb)
                    End If


                Case ListItemType.Footer
                    If controlName = "TEXTBOX" Then
                        Dim txtF As New TextBox()
                        txtF.ID = "txtF" & columnName
                        txtF.Text = ""
                        container.Controls.Add(txtF)
                    End If
                    If controlName = "RADIOBUTTON" Then
                        Dim rad As New RadioButton()
                        rad.Text = ""
                        container.Controls.Add(rad)
                    End If
                    If controlName = "ComboBox" Then
                        Dim ddl As New DropDownList()
                        container.Controls.Add(ddl)
                    End If
                    If controlName = "DateTime" Then
                        Dim txt As New TextBox()
                        Dim Alink As New LinkButton()
                        Dim imag As New System.Web.UI.WebControls.Image()

                        Alink.Attributes.Add("Onclick", "return open_popup('../calendar.aspx?formname=Form1." + columnName + "')")
                        Alink.Attributes.Add("href", "href='../calendar.aspx?formname=Form1." + columnName + "'")
                        imag.ImageUrl = "../images/calender.gif"

                        txt.ID = "txtD" & columnName
                        txt.MaxLength = 13
                        txt.ReadOnly = True
                        txt.Width = Unit.Pixel(130)
                        Alink.Controls.Add(imag)
                        container.Controls.Add(txt)
                        container.Controls.Add(Alink)
                    End If
                    If controlName = "TextArea" Then
                        Dim txt As New TextBox()
                        txt.ID = "txtArea" & columnName
                        txt.AutoPostBack = False
                        txt.TextMode = TextBoxMode.MultiLine
                        txt.Width = Unit.Pixel(200)
                        txt.Height = Unit.Pixel(65)
                        container.Controls.Add(txt)
                    End If
            End Select
        End Sub
        Sub BindStringColumn(ByVal sender As Object, ByVal e As EventArgs)

            Dim lbl2 As Label = CType(sender, Label)

            Dim Container As DataGridItem = CType(lbl2.NamingContainer, DataGridItem)

            lbl2.Text = DataBinder.Eval(Container.DataItem, columnName)

        End Sub

    End Class

    Private Class DataGridTemplateLink
        Implements ITemplate
        Dim templateType As ListItemType
        Dim columnName As String

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

        Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
            Dim lc As New Label()
            Select Case templateType
                Case ListItemType.Header
                    lc.Text = ""
                    container.Controls.Add(lc)
                Case ListItemType.Item
                    Dim Edit As New LinkButton()
                    Edit.CommandName = "Edit"
                    Edit.ID = "lnkEdit"
                    Edit.Text = "Edit"
                    container.Controls.Add(Edit)
                Case ListItemType.EditItem
                    Dim Update As New LinkButton()
                    Update.CommandName = "Update"
                    Update.Text = "Update"
                    Update.ID = "lnkUpdate"
                    Dim Cancel As New LinkButton()
                    Cancel.CommandName = "Cancel"
                    Cancel.ID = "lnkCancel"
                    Cancel.Text = "Cancel"
                    Dim lbl As New Label()
                    lbl.Text = " "
                    container.Controls.Add(Update)
                    container.Controls.Add(lbl)
                    container.Controls.Add(Cancel)
                Case ListItemType.Footer
                    lc.Text = ""
                    container.Controls.Add(lc)
            End Select
        End Sub

    End Class












  Private Sub ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        If e.CommandName = "Update" Then
            Dim strAction As String = ""
            Dim strUser As String = ""
            Dim strUserList As String()
            myDataTable = Session("myDataTable")
            myRow = myDataTable.NewRow
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text

            myDataTable.Rows.Add(myRow)
            Session("myDataTable") = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
        End If

        If e.CommandName = "Add" Then
            Dim strAction As String = ""
            Dim strUser As String = ""
            Dim strUserList As String()
            myDataTable = Session("myDataTable")
            myRow = myDataTable.NewRow
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text
            myRow("KRA") = CType(e.Item.FindControl("txtEKRA"), TextBox).Text

            myDataTable.Rows.Add(myRow)
            Session("myDataTable") = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
        End If
    End Sub


    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)
        Try
            Dim i As Integer
            If e.Item.ItemType = ListItemType.EditItem Then
                For i = 0 To strCaption.Length - 1
                    Select Case strControls(i).ToUpper
                        Case "TEXTBOX"
                            CType(e.Item.FindControl("txtE" & strCaption(i)), TextBox).Text = CType(e.Item.FindControl("lblE" & strCaption(i)), Label).Text


                    End Select
                Next
            End If
        Catch F As Exception
            Response.Write(F.Message)
        End Try
    End Sub

    Private Sub EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        Try


            myDataTable = Session("myDataTable")
            ViewState("EditItemIndex") = e.Item.ItemIndex
            CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = e.Item.ItemIndex
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
            Session("myDataTable") = myDataTable
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        Try
            Dim intcnt
            myDataTable = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = -1
            For Each myRow In myDataTable.Rows
                If intcnt = e.Item.ItemIndex Then
                    myDataTable.Rows.Remove(myRow)
                    myDataTable.AcceptChanges()
                    Exit For
                End If
                intcnt = intcnt + 1
            Next
            myDataTable.AcceptChanges()
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
            Session("myDataTable") = myDataTable
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        Try
            Dim strAction As String = ""
            Dim strUser As String = ""
            Dim strUserList As String()
            myDataTable = Session("myDataTable")
            Dim intcnt
            intcnt = 0
            Dim i As Integer
            For Each myRow In myDataTable.Rows
                If intcnt = e.Item.ItemIndex Then
                    For i = 0 To strCaption.Length - 1
                        Select Case strControls(i).ToUpper
                            Case "TEXTBOX"
                                myRow(strCaption(i)) = CType(e.Item.FindControl("txtE" & strCaption(i)), TextBox).Text


                        End Select
                    Next

                    myDataTable.AcceptChanges()
                    Session("myDataTable") = myDataTable
                    Exit For

                End If
                intcnt = intcnt + 1
            Next
            myDataTable.AcceptChanges()
            Session("myDataTable") = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = -1
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
        Try
            myDataTable = Session("myDataTable")
            CType(Page.FindControl("Datagrid1"), DataGrid).EditItemIndex = -1
            CType(Page.FindControl("Datagrid1"), DataGrid).DataSource = myDataTable
            CType(Page.FindControl("Datagrid1"), DataGrid).DataBind()
            Session("myDataTable") = myDataTable
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub






Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic creation of Columns in datagrid p330418 ASP.NET 2.0 Basics 0 March 29th, 2008 07:33 AM
help required in datagrid dynamic template column govind ASP.NET 1.x and 2.0 Application Design 2 January 4th, 2006 11:32 AM
Datagrid control - Dynamic columns binette99 General .NET 2 September 21st, 2004 12:05 PM
Problem with Dynamic DataGrid athariqbal General .NET 0 March 31st, 2004 01:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.