Create DataGrid at runtime!

Discussion in 'ASP .Net Datagrid Control' started by wannensn, Jun 24, 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" the Event-Handler for the EditCommand is used, clicking on
    "Cancel" no Event-Handler is used;-(

    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:

    ===> WebForm1.aspx:
    <%@ Page Language="vb" AutoEventWireup="false"
    Codebehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1"
    debug="True"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body>
    <form id="Form1" method="post" runat="server">
    &nbsp;</form>
    </body>
    </HTML>

    ==> Then code behind WebForm1.aspx.vb:

    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

    ' Added by hand for access to the form.
    Protected Form1 As System.Web.UI.HtmlControls.HtmlForm
    '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

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

    Private Sub BindGrid()
    'generate some test data
    Dim dTable As 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)


    Dim grid As New DataGrid
    grid.EnableViewState = True
    grid.AutoGenerateColumns = False

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

    Dim gridCol1 As New BoundColumn
    gridCol1.DataField = "Column1"
    gridCol1.HeaderText = "Column1"
    gridCol1.ReadOnly = True
    grid.Columns.Add(gridCol1)

    Dim gridCol2 As New BoundColumn
    gridCol2.DataField = "Column2"
    gridCol2.HeaderText = "Column2"
    grid.Columns.Add(gridCol2)

    grid.DataSource = dTable
    grid.DataBind()

    AddHandler grid.EditCommand, AddressOf grid_EditCommand
    AddHandler grid.CancelCommand, AddressOf grid_CancelCommand
    AddHandler grid.UpdateCommand, AddressOf grid_UpdateCommand

    Form1.Controls.Clear()
    Form1.Controls.Add(grid)

    Session("Recordset") = dTable
    End Sub

    Public Sub grid_EditCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs)
    'set the index of the row to be edited
    Dim grid As DataGrid = CType(source, DataGrid)
    grid.EditItemIndex = CInt(e.Item.ItemIndex)

    'rebind grid
    grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
    grid.DataBind()
    End Sub

    Public Sub grid_CancelCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs)
    'make sure that no row is marked for editing
    Dim grid As DataGrid = CType(source, DataGrid)
    grid.EditItemIndex = -1

    'rebind grid
    grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
    grid.DataBind()
    End Sub

    Public Sub grid_UpdateCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs) 'Handles
    grid.UpdateCommand
    Dim grid As DataGrid = CType(source, DataGrid)

    Dim dTable As Data.DataTable
    dTable = CType(Session("Recordset"), DataTable)

    Dim NewVal As String = CType(e.Item.Cells(2).Controls(0),
    TextBox).Text
    dTable.Rows(grid.EditItemIndex).Item(1) = NewVal
    dTable.AcceptChanges()

    grid.EditItemIndex = -1

    'rebind grid
    grid.DataSource = CType(Session("Recordset"), DataTable).DefaultView
    grid.DataBind()
    End Sub
    End Class
    wannensn, Jun 24, 2005
    #1
    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:
    392
    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,768
    =?Utf-8?B?S2VuIENveCBbTWljcm9zb2Z0IE1WUF0=?=
    Oct 7, 2004
  3. Hal Vaughan
    Replies:
    11
    Views:
    1,081
    Gordon Beaton
    May 22, 2006
  4. Phani
    Replies:
    2
    Views:
    114
    Phani
    Apr 25, 2005
  5. wannensn

    Create DataGrid at runtime

    wannensn, Jul 19, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    277
    Elton Wang
    Jul 19, 2005
Loading...

Share This Page