Create DataGrid at runtime

Discussion in 'ASP .Net Datagrid Control' started by wannensn, Jul 19, 2005.

  1. wannensn

    wannensn Guest

    Hello,

    I need to create a DataGrid at runtime with a EditCommandColumn. Everything
    works fine only the events of the EditCommandColumn, which are not firing
    correctly. If I click on "Edit" the right Event-Handler is used but when I
    click on "Update" or "Cancel" the Event-Handler for the EditCommand is
    used;-(

    For testing I override the "RaisePostBackEvent" of the WebForm: I found out
    that the source of the Event
    is a System.Web.UI.WebControls.DataGridLinkButton. In every case the command
    name
    of the LinkButton is "Edit", if I change this manually to "Update" or
    "Cancel" everything works
    fine. So what could be the problem?!?!

    I read several articles and posts about this, but I never found a solution
    that is working.

    I hope someone can help me.

    Thanks,
    Stephan

    Here is my code:
    Public Class WebForm1
    Inherits System.Web.UI.Page

    #Region " Vom Web Form Designer generierter Code "

    'Dieser Aufruf ist für den Web Form-Designer erforderlich.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    'HINWEIS: Die folgende Platzhalterdeklaration ist für den Web
    Form-Designer erforderlich.
    'Nicht löschen oder verschieben.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: Dieser Methodenaufruf ist für den Web Form-Designer
    erforderlich
    'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
    InitializeComponent()
    End Sub

    #End Region

    Dim mGrid As DataGrid

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    CreateGrid()
    End Sub

    Public Sub CreateGrid()
    'declare a new datagrid and set properties
    mGrid = New DataGrid
    mGrid.ID = "234"
    mGrid.BorderWidth = Unit.Pixel(2)
    mGrid.CellPadding = 10
    mGrid.GridLines = GridLines.Both
    mGrid.BorderColor = Color.Blue
    mGrid.ShowHeader = True
    mGrid.AutoGenerateColumns = False
    mGrid.SelectedItemStyle.BackColor = Color.Yellow

    'add bound columns to the datagrid
    Dim datagridcol As New BoundColumn
    datagridcol.HeaderText = "Column1"
    datagridcol.DataField = "Column1"
    mGrid.Columns.Add(datagridcol)

    datagridcol = New BoundColumn
    datagridcol.HeaderText = "Column2"
    datagridcol.DataField = "Column2"
    mGrid.Columns.Add(datagridcol)

    Dim editCol As New EditCommandColumn
    editCol.ButtonType = ButtonColumnType.LinkButton
    editCol.EditText = "Edit"
    editCol.UpdateText = "Update"
    editCol.CancelText = "Cancel"
    mGrid.Columns.Add(editCol)

    AddHandler mGrid.UpdateCommand, AddressOf DataGrid1_UpdateCommand
    AddHandler mGrid.CancelCommand, AddressOf DataGrid1_CancelCommand
    AddHandler mGrid.EditCommand, AddressOf DataGrid1_EditCommand

    ''bind datagrid
    mGrid.DataSource = GetDataSet()
    mGrid.DataBind()

    'add datagrid to the page
    Page.Controls(1).Controls.Add(mGrid)
    End Sub


    Public Function GetDataSet() As DataTable
    Dim dTable As Data.DataTable = Session.Item("DataTable")
    If dTable Is Nothing Then
    dTable = New Data.DataTable
    Dim dCol1 As New Data.DataColumn("Column1")
    Dim dCol2 As New Data.DataColumn("Column2")

    dTable.Columns.Add(dCol1)
    dTable.Columns.Add(dCol2)

    Dim dRow As Data.DataRow = dTable.NewRow
    dRow.Item("Column1") = "Value1.1"
    dRow.Item("Column2") = "Value1.2"
    dTable.Rows.Add(dRow)

    dRow = dTable.NewRow
    dRow.Item("Column1") = "Value2.1"
    dRow.Item("Column2") = "Value2.2"
    dTable.Rows.Add(dRow)

    Session.Add("DataTable", dTable)
    End If
    Return dTable
    End Function

    Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs)
    Dim tmpGrid As DataGrid = CType(source, DataGrid)
    tmpGrid.EditItemIndex = -1

    'bind datagrid
    tmpGrid.DataSource = GetDataSet()
    tmpGrid.DataBind()
    End Sub

    Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs)
    Dim tmpGrid As DataGrid = CType(source, DataGrid)
    tmpGrid.EditItemIndex = -1

    'bind datagrid
    tmpGrid.DataSource = GetDataSet()
    tmpGrid.DataBind()
    End Sub

    Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs)
    Dim tmpGrid As DataGrid = CType(source, DataGrid)
    tmpGrid.EditItemIndex = CInt(e.Item.ItemIndex)

    'bind datagrid
    tmpGrid.DataSource = GetDataSet()
    tmpGrid.DataBind()
    End Sub

    Protected Overloads Overrides Sub RaisePostBackEvent(ByVal sourceControl As
    System.Web.UI.IPostBackEventHandler, ByVal eventArgument As String)
    CType(sourceControl, System.Web.UI.WebControls.LinkButton).CommandName =
    "Update"
    sourceControl.RaisePostBackEvent(eventArgument)

    End Sub

    End Class
     
    wannensn, Jul 19, 2005
    #1
    1. Advertising

  2. wannensn

    Elton Wang Guest

    Hi Stephan,

    What I can suggest is to use static rather than dynamic EditCommandColumn.
    And in datagrid_ItemDataBound event set it's visible according to condition.


    HTH

    Elton Wang

    "wannensn" <> wrote in message
    news:%...
    > Hello,
    >
    > I need to create a DataGrid at runtime with a EditCommandColumn.

    Everything
    > works fine only the events of the EditCommandColumn, which are not firing
    > correctly. If I click on "Edit" the right Event-Handler is used but when I
    > click on "Update" or "Cancel" the Event-Handler for the EditCommand is
    > used;-(
    >
    > For testing I override the "RaisePostBackEvent" of the WebForm: I found

    out
    > that the source of the Event
    > is a System.Web.UI.WebControls.DataGridLinkButton. In every case the

    command
    > name
    > of the LinkButton is "Edit", if I change this manually to "Update" or
    > "Cancel" everything works
    > fine. So what could be the problem?!?!
    >
    > I read several articles and posts about this, but I never found a solution
    > that is working.
    >
    > I hope someone can help me.
    >
    > Thanks,
    > Stephan
    >
    > Here is my code:
    > Public Class WebForm1
    > Inherits System.Web.UI.Page
    >
    > #Region " Vom Web Form Designer generierter Code "
    >
    > 'Dieser Aufruf ist für den Web Form-Designer erforderlich.
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    >
    > 'HINWEIS: Die folgende Platzhalterdeklaration ist für den Web
    > Form-Designer erforderlich.
    > 'Nicht löschen oder verschieben.
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > 'CODEGEN: Dieser Methodenaufruf ist für den Web Form-Designer
    > erforderlich
    > 'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
    > InitializeComponent()
    > End Sub
    >
    > #End Region
    >
    > Dim mGrid As DataGrid
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Load
    > CreateGrid()
    > End Sub
    >
    > Public Sub CreateGrid()
    > 'declare a new datagrid and set properties
    > mGrid = New DataGrid
    > mGrid.ID = "234"
    > mGrid.BorderWidth = Unit.Pixel(2)
    > mGrid.CellPadding = 10
    > mGrid.GridLines = GridLines.Both
    > mGrid.BorderColor = Color.Blue
    > mGrid.ShowHeader = True
    > mGrid.AutoGenerateColumns = False
    > mGrid.SelectedItemStyle.BackColor = Color.Yellow
    >
    > 'add bound columns to the datagrid
    > Dim datagridcol As New BoundColumn
    > datagridcol.HeaderText = "Column1"
    > datagridcol.DataField = "Column1"
    > mGrid.Columns.Add(datagridcol)
    >
    > datagridcol = New BoundColumn
    > datagridcol.HeaderText = "Column2"
    > datagridcol.DataField = "Column2"
    > mGrid.Columns.Add(datagridcol)
    >
    > Dim editCol As New EditCommandColumn
    > editCol.ButtonType = ButtonColumnType.LinkButton
    > editCol.EditText = "Edit"
    > editCol.UpdateText = "Update"
    > editCol.CancelText = "Cancel"
    > mGrid.Columns.Add(editCol)
    >
    > AddHandler mGrid.UpdateCommand, AddressOf DataGrid1_UpdateCommand
    > AddHandler mGrid.CancelCommand, AddressOf DataGrid1_CancelCommand
    > AddHandler mGrid.EditCommand, AddressOf DataGrid1_EditCommand
    >
    > ''bind datagrid
    > mGrid.DataSource = GetDataSet()
    > mGrid.DataBind()
    >
    > 'add datagrid to the page
    > Page.Controls(1).Controls.Add(mGrid)
    > End Sub
    >
    >
    > Public Function GetDataSet() As DataTable
    > Dim dTable As Data.DataTable = Session.Item("DataTable")
    > If dTable Is Nothing Then
    > dTable = New Data.DataTable
    > Dim dCol1 As New Data.DataColumn("Column1")
    > Dim dCol2 As New Data.DataColumn("Column2")
    >
    > dTable.Columns.Add(dCol1)
    > dTable.Columns.Add(dCol2)
    >
    > Dim dRow As Data.DataRow = dTable.NewRow
    > dRow.Item("Column1") = "Value1.1"
    > dRow.Item("Column2") = "Value1.2"
    > dTable.Rows.Add(dRow)
    >
    > dRow = dTable.NewRow
    > dRow.Item("Column1") = "Value2.1"
    > dRow.Item("Column2") = "Value2.2"
    > dTable.Rows.Add(dRow)
    >
    > Session.Add("DataTable", dTable)
    > End If
    > Return dTable
    > End Function
    >
    > Private Sub DataGrid1_UpdateCommand(ByVal source As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridCommandEventArgs)
    > Dim tmpGrid As DataGrid = CType(source, DataGrid)
    > tmpGrid.EditItemIndex = -1
    >
    > 'bind datagrid
    > tmpGrid.DataSource = GetDataSet()
    > tmpGrid.DataBind()
    > End Sub
    >
    > Private Sub DataGrid1_CancelCommand(ByVal source As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridCommandEventArgs)
    > Dim tmpGrid As DataGrid = CType(source, DataGrid)
    > tmpGrid.EditItemIndex = -1
    >
    > 'bind datagrid
    > tmpGrid.DataSource = GetDataSet()
    > tmpGrid.DataBind()
    > End Sub
    >
    > Private Sub DataGrid1_EditCommand(ByVal source As Object, ByVal e As
    > System.Web.UI.WebControls.DataGridCommandEventArgs)
    > Dim tmpGrid As DataGrid = CType(source, DataGrid)
    > tmpGrid.EditItemIndex = CInt(e.Item.ItemIndex)
    >
    > 'bind datagrid
    > tmpGrid.DataSource = GetDataSet()
    > tmpGrid.DataBind()
    > End Sub
    >
    > Protected Overloads Overrides Sub RaisePostBackEvent(ByVal sourceControl

    As
    > System.Web.UI.IPostBackEventHandler, ByVal eventArgument As String)
    > CType(sourceControl, System.Web.UI.WebControls.LinkButton).CommandName =
    > "Update"
    > sourceControl.RaisePostBackEvent(eventArgument)
    >
    > End Sub
    >
    > End Class
    >
    >
     
    Elton Wang, Jul 19, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kapil edynamic

    create runtime(on fly ) database ?

    kapil edynamic, Aug 23, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    406
    Steve C. Orr, MCSD
    Aug 23, 2003
  2. Big Dave

    Create Datagrid Controls as runtime

    Big Dave, Oct 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    3,786
    =?Utf-8?B?S2VuIENveCBbTWljcm9zb2Z0IE1WUF0=?=
    Oct 7, 2004
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,129
    Gordon Beaton
    May 22, 2006
  4. Phani
    Replies:
    2
    Views:
    127
    Phani
    Apr 25, 2005
  5. wannensn

    Create DataGrid at runtime!

    wannensn, Jun 24, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    148
    wannensn
    Jun 24, 2005
Loading...

Share This Page