R
Roberto C
Hello,
I had a problem that the _EditCommand didn't fire when the event was created in the .vb code. I did like this:
1. I created a datagrid (dgrdProducts) in design view.
2. Then I created the columns in code, like this, the method is called from Page_Load:
Private Sub CreateColumns()
Dim ProductID As New BoundColumn
Dim ProductName As New BoundColumn
Dim UnitPrice As New BoundColumn
Dim EditCol As New EditCommandColumn
With ProductID
.HeaderText = "Product ID"
.DataField = "ProductID"
.ReadOnly = True
End With
With ProductName
.HeaderText = "Product Name"
.DataField = "ProductName"
End With
With UnitPrice
.HeaderText = "Price"
.DataField = "UnitPrice"
.DataFormatString = "{0:c}"
End With
With EditCol
.EditText = "Edit!"
.UpdateText = "Update!"
.CancelText = "Cancel!"
End With
With dgrdProducts.Columns
.Add(ProductID)
.Add(ProductName)
.Add(UnitPrice)
.Add(EditCol)
End With
End Sub
3. I then created the Edit event like this:
Private Sub dgrdProducts_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrdProducts.EditCommand
dgrdProducts.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
End Sub
4. Here is the code for Page_Load and the method BindDataGrid:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conNorthwind = New SqlConnection("Server=localhost;UID=sa;PWD=;Database=Northwind")
With dgrdProducts
.DataKeyField = "ProductID"
.AutoGenerateColumns = False
.CellPadding = 10
.HeaderStyle.BackColor = Color.Salmon
End With
CreateColumns()
If Not IsPostBack Then
BindDataGrid()
End If
End Sub
Private Sub BindDataGrid()
cmdSql = New SqlCommand("Select * From Products", conNorthwind)
conNorthwind.Open()
dgrdProducts.DataSource = cmdSql.ExecuteReader()
dgrdProducts.DataBind()
conNorthwind.Close()
End Sub
When I tried to fire the event, NOTHING happened. It seemed like the page forgot that the edit event existed, strange?
The solution is that I had to create the datagrid in HTML on the .aspx page like this:
<aspataGrid
ID="dgrdProducts"
OnEditCommand="dgrdProducts_EditCommand"
DataKeyField="ProductID"
AutoGenerateColumns="False"
CellPadding="10"
HeaderStyle-BackColor="Salmon"
Runat="Server">
<Columns>
<asp:BoundColumn
HeaderText="Product ID"
DataField="ProductID"
ReadOnly="True" />
<asp:BoundColumn
HeaderText="Product Name"
DataField="ProductName" />
<asp:BoundColumn
HeaderText="Price"
DataField="UnitPrice"
DataFormatString="{0:c}" />
<asp:EditCommandColumn
EditText="Edit!" />
</Columns>
</aspataGrid>
and then I did a small change in the event code, like this (From "Private" to "Protected" and removed "Handles dgrdProducts.EditCommand"), and then everything worked perfect, the event did fire:
Protected Sub dgrdProducts_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
dgrdProducts.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
End Sub
Can someone tell me why it didn't work when I tried to create the event in the .vb code, is that a bug in Framework 1.1? I try not to mess around to much with the HTML, I want to as much as possible in the .vb code.
Thanks in advance!
/Roberto Cusicanqui
I had a problem that the _EditCommand didn't fire when the event was created in the .vb code. I did like this:
1. I created a datagrid (dgrdProducts) in design view.
2. Then I created the columns in code, like this, the method is called from Page_Load:
Private Sub CreateColumns()
Dim ProductID As New BoundColumn
Dim ProductName As New BoundColumn
Dim UnitPrice As New BoundColumn
Dim EditCol As New EditCommandColumn
With ProductID
.HeaderText = "Product ID"
.DataField = "ProductID"
.ReadOnly = True
End With
With ProductName
.HeaderText = "Product Name"
.DataField = "ProductName"
End With
With UnitPrice
.HeaderText = "Price"
.DataField = "UnitPrice"
.DataFormatString = "{0:c}"
End With
With EditCol
.EditText = "Edit!"
.UpdateText = "Update!"
.CancelText = "Cancel!"
End With
With dgrdProducts.Columns
.Add(ProductID)
.Add(ProductName)
.Add(UnitPrice)
.Add(EditCol)
End With
End Sub
3. I then created the Edit event like this:
Private Sub dgrdProducts_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrdProducts.EditCommand
dgrdProducts.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
End Sub
4. Here is the code for Page_Load and the method BindDataGrid:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
conNorthwind = New SqlConnection("Server=localhost;UID=sa;PWD=;Database=Northwind")
With dgrdProducts
.DataKeyField = "ProductID"
.AutoGenerateColumns = False
.CellPadding = 10
.HeaderStyle.BackColor = Color.Salmon
End With
CreateColumns()
If Not IsPostBack Then
BindDataGrid()
End If
End Sub
Private Sub BindDataGrid()
cmdSql = New SqlCommand("Select * From Products", conNorthwind)
conNorthwind.Open()
dgrdProducts.DataSource = cmdSql.ExecuteReader()
dgrdProducts.DataBind()
conNorthwind.Close()
End Sub
When I tried to fire the event, NOTHING happened. It seemed like the page forgot that the edit event existed, strange?
The solution is that I had to create the datagrid in HTML on the .aspx page like this:
<aspataGrid
ID="dgrdProducts"
OnEditCommand="dgrdProducts_EditCommand"
DataKeyField="ProductID"
AutoGenerateColumns="False"
CellPadding="10"
HeaderStyle-BackColor="Salmon"
Runat="Server">
<Columns>
<asp:BoundColumn
HeaderText="Product ID"
DataField="ProductID"
ReadOnly="True" />
<asp:BoundColumn
HeaderText="Product Name"
DataField="ProductName" />
<asp:BoundColumn
HeaderText="Price"
DataField="UnitPrice"
DataFormatString="{0:c}" />
<asp:EditCommandColumn
EditText="Edit!" />
</Columns>
</aspataGrid>
and then I did a small change in the event code, like this (From "Private" to "Protected" and removed "Handles dgrdProducts.EditCommand"), and then everything worked perfect, the event did fire:
Protected Sub dgrdProducts_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
dgrdProducts.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
End Sub
Can someone tell me why it didn't work when I tried to create the event in the .vb code, is that a bug in Framework 1.1? I try not to mess around to much with the HTML, I want to as much as possible in the .vb code.
Thanks in advance!
/Roberto Cusicanqui