Thanks for the quick reply Rajendra, here's my code:
in my aspx I have:
<ASP:DATAGRID id="DisplayDataGrid" runat="server"
OnDeleteCommand="DisplayDataGrid_Delete"
DataKeyField="id"
HeaderStyle-BackColor="#aaaadd" Font-Size="8pt"
Font-Name="Verdana" CellPadding="3"
BorderColor="Black"
BackColor="#CCCCFF" Width="800px"
OnUpdateCommand="DisplayDataGrid_Update"
OnCancelCommand="DisplayDataGrid_Cancel"
OnEditCommand="DisplayDataGrid_Edit"
OnItemDataBound="DisplayDataGrid_ItemDataBound"
AutoGenerateColumns="False"
Font-Names="Verdana" BorderStyle="None">
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
</ASP:DATAGRID>
In my aspx.vb:
Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
sqlconn = New SqlConnection(Application("SQLConnStr"))
If Not (IsPostBack) Then
BindGrid()
End If
End Sub
Sub DisplayDataGrid_Edit(ByVal Sender As Object, ByVal
E As DataGridCommandEventArgs) Handles
DisplayDataGrid.EditCommand
DisplayDataGrid.EditItemIndex = CInt(E.Item.ItemIndex)
BindGrid()
End Sub
Sub DisplayDataGrid_Cancel(ByVal Sender As Object,
ByVal E As DataGridCommandEventArgs) Handles
DisplayDataGrid.CancelCommand
DisplayDataGrid.EditItemIndex = -1
BindGrid()
End Sub
Sub DisplayDataGrid_Update(ByVal Sender As Object,
ByVal E As DataGridCommandEventArgs) Handles
DisplayDataGrid.UpdateCommand
'Some Update codes
BindGrid()
End Sub
Sub DisplayDataGrid_Delete(ByVal Sender As Object,
ByVal E As DataGridCommandEventArgs) Handles
DisplayDataGrid.DeleteCommand
Dim sqlcmd As SqlCommand
Dim DeleteCmd As String = "DELETE from tblMovies where
ID = @Id"
sqlcmd = New SqlCommand(DeleteCmd, sqlconn)
sqlcmd.Parameters.Add(New SqlParameter("@Id",
SqlDbType.Int, 4))
sqlcmd.Parameters("@Id").Value =
DisplayDataGrid.DataKeys(CInt(E.Item.ItemIndex))
sqlcmd.Connection.Open()
Try
sqlcmd.ExecuteNonQuery()
Catch Exc As SqlException
End Try
sqlcmd.Connection.Close()
BindGrid()
End Sub
Sub InitGrid()
Dim DataSetObj As New Data.DataSet
Dim sqlconn As New
SqlClient.SqlConnection(Application("SQLConnStr"))
Dim strSQL As String
Dim iCtr As Integer
Try
Dim btnColumn As New ButtonColumn
btnColumn.HeaderImageUrl =
"images\button_drop.gif"
btnColumn.ButtonType = ButtonColumnType.LinkButton
btnColumn.Text = "X"
btnColumn.CommandName = "Delete"
DisplayDataGrid.Columns.Add(btnColumn)
'Check for Authentication, then set the visibility
for Delete & Edit Columns:
'DisplayDataGrid.Columns(0).Visible = False
Dim editCol As New EditCommandColumn
editCol.HeaderText = "Edit"
editCol.ButtonType = ButtonColumnType.LinkButton
editCol.UpdateText = "Update"
editCol.EditText = "Edit"
editCol.CancelText = "Cancel"
DisplayDataGrid.Columns.Add(editCol)
AddHandler DisplayDataGrid.DeleteCommand, New
DataGridCommandEventHandler(AddressOf
DisplayDataGrid_Delete)
AddHandler DisplayDataGrid.EditCommand, New
DataGridCommandEventHandler(AddressOf
DisplayDataGrid_Edit)
AddHandler DisplayDataGrid.UpdateCommand, New
DataGridCommandEventHandler(AddressOf
DisplayDataGrid_Update)
AddHandler DisplayDataGrid.CancelCommand, New
DataGridCommandEventHandler(AddressOf
DisplayDataGrid_Cancel)
strSQL = "SELECT * FROM GUI_tblDisplay WHERE
Groups = 'Edit' ORDER BY DisplayOrder"
sqlconn.Open()
Dim sqlDataSet As New
SqlClient.SqlDataAdapter(strSQL, sqlconn)
sqlDataSet.Fill(DataSetObj, "table")
For iCtr = 0 To
DataSetObj.Tables("table").Rows.Count - 1
Dim tc As New TemplateColumn
tc.ItemTemplate = New
clsDGItemTemplate(DataSetObj.Tables("table").Rows(iCtr).Item("FieldName"))
tc.HeaderText =
CStr(DataSetObj.Tables("table").Rows(iCtr).Item("DisplayName"))
'tc.HeaderStyle.Width =
System.Web.UI.WebControls.Unit.Pixel(200)
tc.EditItemTemplate = New
clsDGEditItemTemplate(DataSetObj.Tables("table").Rows(iCtr).Item("FieldName"))
DisplayDataGrid.Columns.Add(tc)
Next
Catch exp As Exception
Response.Write("Error in initializing datagrid!
edit.InitGrid() " & exp.ToString)
Catch sql_exp As SqlClient.SqlException
Response.Write("Error in initializing datagrid!
edit.InitGrid() " & sql_exp.ToString)
Finally
Try
sqlconn.Close()
DataSetObj.Dispose()
sqlconn = Nothing
DataSetObj = Nothing
Catch exp As Exception
End Try
End Try
End Sub
Sub BindGrid()
InitGrid()
Dim DS As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = New SqlDataAdapter("select * from
tblMovies", sqlconn)
DS = New DataSet
MyCommand.Fill(DS, "Movies")
DisplayDataGrid.DataSource =
DS.Tables("Movies").DefaultView
DisplayDataGrid.DataBind()
End Sub
It won't detect both delete command and edit command
--- Rajendra Appalla <[EMAIL PROTECTED]>
wrote:
> I don't see why the events of the buttons does not
> fire, as you said you
> are adding the columns to the datagrid even in the
> postback.
> May be if you post your code, we can find something.
>
> Rajendra.
>
> -----Original Message-----
> From: Psychomaniac
> [mailto:[EMAIL PROTECTED]
> Sent: Thursday, September 23, 2004 9:56 AM
> To: [EMAIL PROTECTED]
> Subject: Re: [AspNetAnyQuestionIsOk] Dynamic
> Datagrids
>
> I'm using Codebehind. The reason for this is because
> the datagrid will show columns that's specified in
> the
> database. Thus it can only be created during
> run-time.
>
> --- Ben Miller <[EMAIL PROTECTED]> wrote:
>
> > One other question. Are you using Codebehind or
> > inline code?
> >
> > This matters because on inline code, the events
> are
> > hooked up automatically, and codebehind can get
> > change things based on whether there is a button
> on
> > the datagrid at compiletime.
> >
> > Ben Miller
> >
> > ----- Original Message -----
> > From: Psychomaniac
> > To: [EMAIL PROTECTED]
> > Sent: Wednesday, September 22, 2004 10:08 AM
> > Subject: RE: [AspNetAnyQuestionIsOk] Dynamic
> > Datagrids
> >
> >
> > Yes, I actually still have those (OnEditCommand,
> > OnCancelCommand and OnUpdateCommand)
> >
> >
> >
> > --- Rajendra Appalla
> <[EMAIL PROTECTED]>
> > wrote:
> >
> > > I hope you still have this in the HTML part of
> > the
> > > datagrid.
> > >
> > > OnEditCommand="ItemsGrid_Edit"
> > > OnCancelCommand="ItemsGrid_Cancel"
> > > OnUpdateCommand="ItemsGrid_Update"
> > >
> > > Rajendra.
> > >
> > > -----Original Message-----
> > > From: Psychomaniac
> > > [mailto:[EMAIL PROTECTED]
> > > Sent: Wednesday, September 22, 2004 11:33 AM
> > > To: [EMAIL PROTECTED]
> > > Subject: RE: [AspNetAnyQuestionIsOk] Dynamic
> > > Datagrids
> > >
> > > Yes, I did that. But actually, those two piece
> > of
> > > code
> > > did not even fire up any events the first time
> > > called.
> > >
> > >
> > >
> > >
> > > --- Rajendra Appalla
> > <[EMAIL PROTECTED]>
> > > wrote:
> > >
> > > > Are you adding those two columns again in
> > > PostBack??
> > > > Because you
> > > > should. The known behavior if you don't add
> > those
> > > > columns again in
> > > > postback is that the events won't fire on
> > those
> > > > columns.
> > > >
> > > > Rajendra.
> > > >
> > > > -----Original Message-----
> > > > From: Psychomaniac
> > > > [mailto:[EMAIL PROTECTED]
> > > > Sent: Wednesday, September 22, 2004 5:39 AM
> > > > To: [EMAIL PROTECTED]
> > > > Subject: [AspNetAnyQuestionIsOk] Dynamic
> > Datagrids
> > > >
> > > > Hi, I'm trying to build an ASP.NET display
> > page
> > > > using
> > > > datagrid. This datagrid must contain:
> > ButtonColumn
> > > &
> > > > EditCommandColumn. This is part of my code:
> > > >
> > > > <asp:DataGrid id="ItemsGrid"
> > > > BorderColor="black"
> > > > BorderWidth="1"
> > > > CellPadding="3"
> > > > OnEditCommand="ItemsGrid_Edit"
> > > >
> OnCancelCommand="ItemsGrid_Cancel"
> > > >
> OnUpdateCommand="ItemsGrid_Update"
> > > > OnItemCommand="ItemsGrid_Command"
> > > > AutoGenerateColumns="false"
> > > > runat="server">
> > > >
> > > > <HeaderStyle BackColor="#aaaadd">
> > > > </HeaderStyle>
> > > >
> > > > <Columns>
> > > >
> > > > <asp:EditCommandColumn
> > > > EditText="Edit"
> > > > CancelText="Cancel"
> > > > UpdateText="Update"
> > > > HeaderText="Edit item">
> > > >
> > > > <ItemStyle Wrap="False">
> > > > </ItemStyle>
> > > >
> > > > <HeaderStyle Wrap="False">
> > > > </HeaderStyle>
> > > >
> > > > </asp:EditCommandColumn>
> > > >
> > > > <asp:ButtonColumn
> > > > HeaderText="Delete item"
> > > > ButtonType="LinkButton"
> > > > Text="Delete"
> > > > CommandName="Delete"/>
> > > >
> > > > Note that I also have defined
> > "ItemsGrid_Edit",
> > > > "ItemsGrid_Cancel", "ItemsGrid_Update" and
> > > > "ItemsGrid_Command" in my VB code.
> > > >
> > > > It all works fine. However, I want to create
> > the
> > > > ButtonColumn & EditCommandColumn dynamically
> > (i.e.
> > > > during run-time). So I removes those 2
> pieces
> > of
> > > ASP
> > > > code (asp:ButtonColumn &
> > asp:EditCommandColumn)
> > > from
> > > > the aspx, and in my VB code, I add this:
> > > >
> > > > Dim btnColumn As New ButtonColumn
> > > > btnColumn.ButtonType =
> > ButtonColumnType.LinkButton
> > > > btnColumn.HeaderText = "Delete item"
> > > > btnColumn.Text = "Delete"
> > > > btnColumn.CommandName = "Delete"
> > > >
> > > > Dim editColumn As New EditCommandColumn
> > > > editColumn.HeaderText = "Edit Item"
> > > > editColumn.ButtonType =
> > > ButtonColumnType.LinkButton
> > > > editColumn.UpdateText = "Update"
> > > > editColumn.CancelText = "Cancel"
> > > > editColumn.EditText = "E"
> > > > editColumn.ItemStyle.Wrap = False
> > > >
> > > > ItemsGrid.Columns.Add(btnColumn)
> > > > ItemsGrid.Columns.Add(editColumn)
> > > >
> > > > At first, it displays the grid correctly,
> > however,
> > > > when I click on "Delete" or the "Edit" link
> > > button,
> > > > it
> > > > never raises the event OnDeleteCommand nor
> > > > OnEditCommand!
> > > >
> > > > Has anybody experienced this kind of problem
> > as
> > > > well?
> > > > Thanks a lot!
> > > >
> > > > Adrian
>
=== message truncated ===
__________________________________
Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.
http://mobile.yahoo.com/maildemo
------------------------ Yahoo! Groups Sponsor --------------------~-->
$9.95 domain names from Yahoo!. Register anything.
http://us.click.yahoo.com/J8kdrA/y20IAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/