Changing Datagrid columns at runtime

G

Georg Scholz

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
 
J

Jos

Georg said:
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.
 
G

Georg Scholz

Jos said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top