update command does not fire when adding row

R

Rock

I have built a grid dynamically, and with edit, delete and bound columns. I
can edit the records in the grid with no problem; when you click on Edit,
the column changes to Update & Cancel, enter your changes and click update,
and the datagrid update command fires.

I also have an Add button that adds a blank row to the datagrid, also with
the options Update & Cancel, user enters their additions, clicks Update and
all works fine.

What doesn't work, however, is if there are no records in the grid to begin
with. The grid is displayed with just the header showing, and a lbl stating
there are no records found. Click Add, as above, which adds a blank row to
the datagrid, also with the options Update & Cancel. However, when user
clicks Update, this time the datagrid update command does not fire and the
record is not saved.

What do I need to do to solve this? The Update command isn't even firing,
and works correctly when records already exist in the datagrid. It's as if
the datagrid isn't created, but it has been created, only just the columns
headings are displayed.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Call CreateGrid()
Call CreateColumns()
Call GetData()
End Sub

Private Sub CreateGrid
With dg
.AutoGenerateColumns = "False"
.CellPadding="4"
.BackColor=Color.White
.BorderColor=Color.FromName("#336666")
'#33666
.BorderWidth = Unit.Pixel(3)
.GridLines=GridLines.Horizontal
.BorderStyle=BorderStyle.Double
.ShowHeader = true
.HeaderStyle.BackColor = Color.FromName("#336666")
.HeaderStyle.ForeColor = Color.White
.HeaderStyle.Font.Bold = true
.EditItemStyle.BackColor = Color.Silver
End With

'add event handlers
AddHandler dg.EditCommand, AddressOf dg_EditCommand
AddHandler dg.CancelCommand, AddressOf dg_CancelCommand
AddHandler dg.DeleteCommand, AddressOf dg_DeleteCommand
AddHandler dg.UpdateCommand, AddressOf dg_UpdateCommand

'bind grind and add to the page
dg.DataBind
PlaceHolder1.controls.Add(dg)
end sub

Private Sub CreateColumns
Dim DGEditColumn as New EditCommandColumn
With DGEditColumn
.ButtonType=ButtonColumnType.LinkButton
.UpdateText="Update"
.HeaderText="Edit"
.CancelText="Cancel"
.EditText="Edit"
end with
dg.Columns.Add(DGEditColumn)

Dim DGBoundColumnLastName as New BoundColumn
With DGBoundColumnID
.Visible=True
.DataField="LastName"
End With
dg.Columns.Add(DGBoundColumnID)
end Sub

Private Sub GetData()
Dim ds As DataSet
Dim qry As String
qry = GetQuery
ds = GetDataSet(qry, strConn)
dim dt as DataTable = ds.Tables(0)
lblStatus.Text = ""
Session("DS") = ds
dg.DataSource = ds

dg.DataBind()
End Sub

Private Sub lnkAdd_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lnkAdd.Click
Call AddRecord()
End Sub
Private Sub AddRecord()
Dim ds As DataSet
Dim dt As DataTable
Dim dr As DataRow
ds = CType(Session("DS"), DataSet)
'copies only the structure of the dataset, no data
dt = ds.Tables(0).Clone
dr = dt.NewRow()
dt.Rows.Add(dr)

With dg
.EditItemIndex = 0
.DataSource = dt
.DataBind()
End With

Session("Mode") = "Add"
lnkAdd.Visible = False
End Sub
 
S

Saravana

Are you binding the datagrid in page_load without checking for ispostback()
property.
 
R

Rock

Yes, but it works fine on a datagrid that has records.

I have to bind the grid on every page load, otherwise I lose the data when
the user edits a row.

The difference I have found is I have a CustomColumn class that builds the
datagrid that overrides the initializecell and contains the ItemDataBinding
and EditItemDataBinding. When a datagrid with records is edited, added to
or updated, the ItemDataBinding events are fired, along with the
InitializeCell. When a datagrid with no records is added to, however, these
events do not fire. I cannot see what these events do though that would
still prevent the update command from firing:

Public Overrides Sub InitializeCell(ByVal cell As TableCell, ByVal
columnIndex As Integer, ByVal itemType As ListItemType)
MyBase.InitializeCell(cell, columnIndex, itemType)
Select Case itemType
Case ListItemType.Header
cell.Text = HeaderText
Case ListItemType.Item, ListItemType.AlternatingItem
AddHandler cell.DataBinding, AddressOf ItemDataBinding
Case ListItemType.EditItem
AddHandler cell.DataBinding, AddressOf
EditItemDataBinding
Dim DDL As New DropDownList
cell.Controls.Add(DDL)
End Select
End Sub

Private Sub ItemDataBinding(ByVal sender As Object, ByVal e As
EventArgs)
Dim cell As TableCell = CType(sender, TableCell)
Dim DGI As DataGridItem = CType(cell.NamingContainer,
DataGridItem)
Try
cell.Text = DGI.DataItem(DataField)
Catch RangeEx As IndexOutOfRangeException
Throw New Exception("Specified DataField was not found.")
Catch OtherEx As Exception
Throw New Exception("Other Exception")
End Try
End Sub
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top