I
Iams
I am having a heck of a time getting a child grid (a grid within a grid) to
go into edit mode. My code is below; it will go into edit mode only after
clicking on the dgChild's Edit linkbutton twice, and I can't figure out what
it's doing on the second click that it's not doing on the first click. If I
rebind the grid in the dgChild_EditCommand event, I lose all of the child
grid's data, and it displays only the parent grid records. If I rebind the
parent grid, it appears to do nothing since it resets everything. Any ideas
how I should be doing this? Another note: On the first click, under the
page load, the Session("Mode") is not yet set to "Edit" since the page loads
before the EditCommand event fires, but the "Guid/PrevGuid" session settings
prevent the grid from rebinding here. So there still is nothing different
that I can find that gets fired the second time, which successfully goes
into edit mode.
tia
<asp:datagrid id="dg" EnableViewState=True runat="server"
BorderStyle="Double" GridLines="Horizontal" BorderWidth="3px"
BorderColor="#cccccc" BackColor="White" CellPadding="4"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ItemStyle-VerticalAlign=Top ButtonType="LinkButton"
UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:EditCommandColumn ItemStyle-VerticalAlign=Top ButtonType="LinkButton"
UpdateText="Confirm Delete" SortExpression="1" HeaderText="Delete"
CancelText="Cancel" EditText="Delete"></asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="ID"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn ItemStyle-VerticalAlign=Top HeaderText="Utility"
DataField="Utility"></asp:BoundColumn>
<asp:BoundColumn ItemStyle-VerticalAlign=Top HeaderText="AccountNumber"
DataField="AccountNumber"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Utility Records">
<ItemTemplate>
<aspataGrid id="dgChild"
AutoGenerateColumns="False"
OnEditCommand="dgChild_EditCommand"
EnableViewState="True"
Runat="server" DataSource='<%#
GetChildRelationDataSource(Container.DataItem, "relUtilityRecords") %>'>
<columns>
<asp:EditCommandColumn ItemStyle-VerticalAlign=Top
ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:EditCommandColumn ItemStyle-VerticalAlign=Top
ButtonType="LinkButton" UpdateText="Confirm Delete" SortExpression="1"
HeaderText="Delete" CancelText="Cancel"
EditText="Delete"></asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="ID"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Yr"
HeaderText="Year"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Comments"
DataField="Comments"></asp:BoundColumn>
<asp:BoundColumn HeaderText="UtilityID"
DataField="UtilityID"></asp:BoundColumn>
</columns>
</aspataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></COLUMNS>
...........
Code Behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Page.SmartNavigation = True
If Not ispostback
Dim strGuid as String = HTTPContext.Current.Session("Guid")
GetData(qry)
else
If Session("Mode") = "Edit" then
'dgChild.Columns(1).Visible = false
else
If HTTPContext.Current.Session("Guid") =
HTTPContext.Current.Session("PrevGuid")
else
GetData(qry)
End If
end if
'Reset session values
HTTPContext.Current.Session("PrevGuid") =
HTTPContext.Current.Session("Guid")
End Sub
Private Sub GetData(qry)
Dim ds As New DataSet
ds = GetDataSet(qry, strConn)
' Try
HTTPContext.Current.Session("DS") = ds
dg.DataSource = ds
dg.DataBind()
' Catch ex as IndexOutOfRangeException
' Session("Status") = ex.Message & ": " & qry
' Catch ex as System.Web.HttpException
' Session("Status") = ex.Message
'End Try
End Sub
Public Shared Function GetDataSet(ByVal SQLString As String, ByVal
ConnectionString As String) As DataSet
Dim da As sqlDataAdapter
Dim ds As new DataSet
'Try
da = New sqlDataAdapter(SQLString, ConnectionString)
da.Fill(ds, "tblUtility")
ds.Relations.Add("relUtility", ds.Tables(0).Columns("ID"),
ds.Tables(1).Columns("UtilityID"))
ds.Relations(0).Nested = true
'Catch ex as Exception
' HTTPContext.Current.Session("Status") = ex.Message
'End Try
Return ds
End Function
Public Sub dgChild_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgChild.EditCommand
Session("Mode")="Edit"
EditCommand(source, e)
End Sub
Public Sub EditCommand(dgrid, e)
Dim strMode as String = HTTPContext.Current.Session("Mode")
Dim ds as new dataset
ds = CType(HTTPContext.Current.Session("DS"),
DataSet)
'If strMode = "Delete" then
' dgrid.Columns(0).Visible = False
'Elseif strMode = "Edit" then
' dgrid.Columns(1).Visible = False
'end if
Dim dv As DataView
dv = ds.Tables(1).DefaultView
dgrid.EditItemIndex = e.Item.ItemIndex
End Sub
go into edit mode. My code is below; it will go into edit mode only after
clicking on the dgChild's Edit linkbutton twice, and I can't figure out what
it's doing on the second click that it's not doing on the first click. If I
rebind the grid in the dgChild_EditCommand event, I lose all of the child
grid's data, and it displays only the parent grid records. If I rebind the
parent grid, it appears to do nothing since it resets everything. Any ideas
how I should be doing this? Another note: On the first click, under the
page load, the Session("Mode") is not yet set to "Edit" since the page loads
before the EditCommand event fires, but the "Guid/PrevGuid" session settings
prevent the grid from rebinding here. So there still is nothing different
that I can find that gets fired the second time, which successfully goes
into edit mode.
tia
<asp:datagrid id="dg" EnableViewState=True runat="server"
BorderStyle="Double" GridLines="Horizontal" BorderWidth="3px"
BorderColor="#cccccc" BackColor="White" CellPadding="4"
AutoGenerateColumns="False">
<Columns>
<asp:EditCommandColumn ItemStyle-VerticalAlign=Top ButtonType="LinkButton"
UpdateText="Update" HeaderText="Edit" CancelText="Cancel"
EditText="Edit"></asp:EditCommandColumn>
<asp:EditCommandColumn ItemStyle-VerticalAlign=Top ButtonType="LinkButton"
UpdateText="Confirm Delete" SortExpression="1" HeaderText="Delete"
CancelText="Cancel" EditText="Delete"></asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="ID"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn ItemStyle-VerticalAlign=Top HeaderText="Utility"
DataField="Utility"></asp:BoundColumn>
<asp:BoundColumn ItemStyle-VerticalAlign=Top HeaderText="AccountNumber"
DataField="AccountNumber"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Utility Records">
<ItemTemplate>
<aspataGrid id="dgChild"
AutoGenerateColumns="False"
OnEditCommand="dgChild_EditCommand"
EnableViewState="True"
Runat="server" DataSource='<%#
GetChildRelationDataSource(Container.DataItem, "relUtilityRecords") %>'>
<columns>
<asp:EditCommandColumn ItemStyle-VerticalAlign=Top
ButtonType="LinkButton" UpdateText="Update" HeaderText="Edit"
CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:EditCommandColumn ItemStyle-VerticalAlign=Top
ButtonType="LinkButton" UpdateText="Confirm Delete" SortExpression="1"
HeaderText="Delete" CancelText="Cancel"
EditText="Delete"></asp:EditCommandColumn>
<asp:BoundColumn Visible="False" DataField="ID"
ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="Yr"
HeaderText="Year"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Comments"
DataField="Comments"></asp:BoundColumn>
<asp:BoundColumn HeaderText="UtilityID"
DataField="UtilityID"></asp:BoundColumn>
</columns>
</aspataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></COLUMNS>
...........
Code Behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Page.SmartNavigation = True
If Not ispostback
Dim strGuid as String = HTTPContext.Current.Session("Guid")
GetData(qry)
else
If Session("Mode") = "Edit" then
'dgChild.Columns(1).Visible = false
else
If HTTPContext.Current.Session("Guid") =
HTTPContext.Current.Session("PrevGuid")
else
GetData(qry)
End If
end if
'Reset session values
HTTPContext.Current.Session("PrevGuid") =
HTTPContext.Current.Session("Guid")
End Sub
Private Sub GetData(qry)
Dim ds As New DataSet
ds = GetDataSet(qry, strConn)
' Try
HTTPContext.Current.Session("DS") = ds
dg.DataSource = ds
dg.DataBind()
' Catch ex as IndexOutOfRangeException
' Session("Status") = ex.Message & ": " & qry
' Catch ex as System.Web.HttpException
' Session("Status") = ex.Message
'End Try
End Sub
Public Shared Function GetDataSet(ByVal SQLString As String, ByVal
ConnectionString As String) As DataSet
Dim da As sqlDataAdapter
Dim ds As new DataSet
'Try
da = New sqlDataAdapter(SQLString, ConnectionString)
da.Fill(ds, "tblUtility")
ds.Relations.Add("relUtility", ds.Tables(0).Columns("ID"),
ds.Tables(1).Columns("UtilityID"))
ds.Relations(0).Nested = true
'Catch ex as Exception
' HTTPContext.Current.Session("Status") = ex.Message
'End Try
Return ds
End Function
Public Sub dgChild_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgChild.EditCommand
Session("Mode")="Edit"
EditCommand(source, e)
End Sub
Public Sub EditCommand(dgrid, e)
Dim strMode as String = HTTPContext.Current.Session("Mode")
Dim ds as new dataset
ds = CType(HTTPContext.Current.Session("DS"),
DataSet)
'If strMode = "Delete" then
' dgrid.Columns(0).Visible = False
'Elseif strMode = "Edit" then
' dgrid.Columns(1).Visible = False
'end if
Dim dv As DataView
dv = ds.Tables(1).DefaultView
dgrid.EditItemIndex = e.Item.ItemIndex
End Sub