Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Create DataGrid at runtime

Reply
Thread Tools

Create DataGrid at runtime

 
 
wannensn
Guest
Posts: n/a
 
      07-19-2005
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


 
Reply With Quote
 
 
 
 
Elton Wang
Guest
Posts: n/a
 
      07-19-2005
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime.exec(String[]) Doesn't Always Work, bBut Runtime.exec(String) Does Hal Vaughan Java 11 05-22-2006 04:49 PM
Create DataGrid at runtime! wannensn ASP .Net Datagrid Control 0 06-24-2005 06:46 AM
Need to create Hieararical DataGrid using ASP.Net Datagrid !!! Phani ASP .Net Datagrid Control 2 04-25-2005 12:08 PM
How to add a Dropdown list to a datagrid at runtime (dynamic) without using template columns in ASP.NET and still have the ability to us the datagrid Update event. Daniel Roth ASP .Net Datagrid Control 0 04-05-2005 03:58 AM
Create Datagrid Controls as runtime Big Dave ASP .Net 1 10-07-2004 07:05 PM



Advertisments