Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Datagrid Control (http://www.velocityreviews.com/forums/f60-asp-net-datagrid-control.html)
-   -   Create DataGrid at runtime (http://www.velocityreviews.com/forums/t762154-create-datagrid-at-runtime.html)

wannensn 07-19-2005 01:13 PM

Create DataGrid at runtime
 
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 07-19-2005 03:41 PM

Re: Create DataGrid at runtime
 
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" <wanews@reitec.de> wrote in message
news:%23vw6nOGjFHA.2644@TK2MSFTNGP09.phx.gbl...
> 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
>
>





All times are GMT. The time now is 05:37 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.