Professional Web Applications Themes

Changing Datagrid columns at runtime - ASP.NET Data Grid Control

Dear all, I'm writing an ASP.NET application, which allows flexible editing of tables stored in an oracle database. Therefore, it is necessary to create all the bound columns at RUNTIME. So far, I'm already able to create bound columns and also to create an "Edit" Button column. The problem is, whenever you are clicking on "Edit", then the DataGrid switches to the original layout which is defined in the Visual Studio designer! In other words, all columns get lost, and I get a complete other column design. Somehow the DataGrid seems to distinguish between an "Select Mode" and an "Edit ...

  1. #1

    Default Changing Datagrid columns at runtime

    Dear all,

    I'm writing an ASP.NET application, which allows flexible editing of
    tables stored in an oracle database. Therefore, it is necessary to
    create all the bound columns at RUNTIME.

    So far, I'm already able to create bound columns and also to create an
    "Edit" Button column.

    The problem is, whenever you are clicking on "Edit", then the DataGrid
    switches to the original layout which is defined in the Visual Studio
    designer! In other words, all columns get lost, and I get a complete
    other column design.

    Somehow the DataGrid seems to distinguish between an "Select Mode" and
    an "Edit Mode". However, I wasn't able to get out how this Edit Mode is
    defined!?

    I'm posting some code below.
    Any help is highly appreciated!

    Thanks in advance

    Georg Scholz


    Imports System.Data.OleDb
    Imports System.Diagnostics

    Public Class WebForm1
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "
    [...]
    #End Region


    ' Everything is starting here
    ' After the Users clicks a button,
    ' dynamically create Bound columns, then load data.
    '
    ' This is a hard-coded test case here.
    ' Normally, you would create the columns by iterating through
    ' the dataset's columns.

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles btnSQL.Click

    Me.msgrid.Columns.Clear()

    Dim colBound As New System.Web.UI.WebControls.BoundColumn
    colBound.DataField = "CCKEY"
    colBound.HeaderText = "CCKEY"
    colBound.Initialize()
    Me.msgrid.Columns.Add(colBound)

    colBound = New System.Web.UI.WebControls.BoundColumn
    colBound.DataField = "CCDESC"
    colBound.HeaderText = "CCDESC"
    Me.msgrid.Columns.Add(colBound)

    Dim colEdit As New System.Web.UI.WebControls.EditCommandColumn
    colEdit.ButtonType = ButtonColumnType.PushButton
    colEdit.EditText = "Edit"
    colEdit.CancelText = "Cancel"
    Me.msgrid.Columns.Add(colEdit)

    UpdateView()
    End Sub



    Private Sub UpdateView()
    Dim strSQL As String = "SELECT CCKEY, CCDESC FROM T_COSTCENTERS"
    Dim conn As OleDbConnection = New OleDbConnection("Provider=...)
    Dim da As New OleDbDataAdapter(strSQL, conn)

    Dim ds As New DataSet
    da.Fill(ds, "MyTable")

    Me.msgrid.DataSource = ds.Tables("MyTable")
    Me.msgrid.DataBind()
    End Sub


    Private Sub msgrid_EditCommand(ByVal source As Object, ByVal e As
    System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
    msgrid.EditCommand
    msgrid.EditItemIndex = e.Item.ItemIndex
    UpdateView()
    End Sub


    End Class







    Georg Scholz Guest

  2. #2

    Default Re: Changing Datagrid columns at runtime

    Georg Scholz wrote:
    > Dear all,
    >
    > I'm writing an ASP.NET application, which allows flexible editing of
    > tables stored in an oracle database. Therefore, it is necessary to
    > create all the bound columns at RUNTIME.
    >
    > So far, I'm already able to create bound columns and also to create an
    > "Edit" Button column.
    >
    > The problem is, whenever you are clicking on "Edit", then the DataGrid
    > switches to the original layout which is defined in the Visual Studio
    > designer! In other words, all columns get lost, and I get a complete
    > other column design.
    >
    > Somehow the DataGrid seems to distinguish between an "Select Mode" and
    > an "Edit Mode". However, I wasn't able to get out how this Edit Mode
    > is defined!?
    It has nothing to do with "Select" or "Edit".
    As a rule, when you post back, all the *dynamic* changes to a page are lost.
    This is true for all the controls on a page, not only for grids.

    Therefore, you need to apply the same dynamic changes
    AFTER postback.

    I suggest you add some values to Viewstate to keep track
    of the state of your columns. Then, use these variables to
    build the columns again at postback.

    --

    Jos


    Jos Guest

  3. #3

    Default Re: Changing Datagrid columns at runtime

    Jos wrote:
    > Georg Scholz wrote:
    [...]
    >> Therefore, it is necessary to create all the bound columns at RUNTIME.
    [...]
    >>The problem is, whenever you are clicking on "Edit", then the DataGrid
    >>switches to the original layout which is defined in the Visual Studio
    >>designer! In other words, all columns get lost, and I get a complete
    >>other column design.
    [...]
    > It has nothing to do with "Select" or "Edit".
    > As a rule, when you post back, all the *dynamic* changes to a page are lost.
    > This is true for all the controls on a page, not only for grids.
    >
    > Therefore, you need to apply the same dynamic changes
    > AFTER postback.
    [...]

    Thanks for your answer ... of course, this was the reason. And yes, this
    openend up a complete new door for my understanding of the ASP.NET
    progamming model.

    Georg Scholz

    Georg Scholz Guest

Similar Threads

  1. How to add columns to AdvancedDataGrid at runtime?
    By Pratap Reddy in forum Macromedia Flex General Discussion
    Replies: 5
    Last Post: May 14th, 12:04 PM
  2. Creation and Positioning DataGrid Columns at runtime.
    By Alan Seunarayan in forum ASP.NET Data Grid Control
    Replies: 5
    Last Post: July 27th, 12:59 PM
  3. Replies: 0
    Last Post: April 5th, 03:58 AM
  4. Adding Columns at runtime in datagrid control
    By Naveed Alam in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: September 5th, 08:23 AM
  5. Columns and Inherited Datagrid...Active Schema does not support columns
    By rob thomson in forum ASP.NET Data Grid Control
    Replies: 0
    Last Post: September 4th, 03:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139