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
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