GridView disapears after entering edit mode.

M

Michael

Hi Everyone,
I'm having a problem with the gridview control. When I click on the edit
link, the grid disapears and don't come back, until I restart the page. I
have setup the grid as such:
<asp:GridView ID="grdPOs" runat="server" AllowPaging="True"
AutoGenerateColumns="False"
BackColor="White" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px"
CellPadding="4" Width="692px" DataKeyNames="ItemId">
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<Columns>
<asp:BoundField DataField="Page" HeaderText="Page"
SortExpression="Page">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="CatalogId" HeaderText="Catalog #">
<ItemStyle HorizontalAlign="Right" Width="80px" />
</asp:BoundField>
<asp:BoundField DataField="ItemDescription"
HeaderText="Description" SortExpression="ItemDescription">
<ItemStyle Width="250px" />
</asp:BoundField>
<asp:BoundField DataField="Qty" HeaderText="Qty"
SortExpression="Qty">
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
<asp:BoundField DataField="UnitPrice"
DataFormatString="{0:C}" HeaderText="UnitPrice"
SortExpression="UnitPrice">
<ItemStyle HorizontalAlign="Right" Width="70px" />
</asp:BoundField>
<asp:BoundField DataFormatString="{0:0.00}"
HeaderText="Total" InsertVisible="False" />
<asp:CommandField ShowEditButton="True" />
<asp:ButtonField CommandName="Delete" Text="Delete" />
<asp:BoundField DataField="POrderId" HeaderText="POrderId"
InsertVisible="False"
SortExpression="POrderId" Visible="False" />
<asp:BoundField DataField="OrderDate" HeaderText="OrderDate"
SortExpression="OrderDate"
Visible="False" />
<asp:BoundField DataField="ItemID" HeaderText="ItemID"
InsertVisible="False" SortExpression="ItemID"
Visible="False" />
</Columns>
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True"
ForeColor="#663399" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099"
HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True"
ForeColor="#FFFFCC" />
</asp:GridView>

And the RowEditing event is as follows.
Protected Sub grdPOs_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdPOs.RowEditing
grdPOs.EditIndex = e.NewEditIndex
DataBind()
End Sub

Has anyone else seen anything like this. Thanks for any advice that you can
provide.
Michael
 
C

Christopher Reed

Where is RowEditing being invoked in your GridView declarative statement?
Plus, where's the data source?
 
M

Michael

Actually, I'm using a dataset to work with the screen. I have setup two
columns to handle the Delete and Edit functions for the grid:
<asp:CommandField ShowEditButton="True" />
<asp:ButtonField CommandName="Delete" Text="Delete" />

Here is the code for the class(Code-Behind) minus a few saving functions to
save some space. Here is the code:
Imports System.Data
imports System.Data.SqlClient


Partial Class EnterPOrder
Inherits System.Web.UI.Page

Private NetCon as SqlConnection
Private dsPO as New DataSet
Private dsPrograms as New DataSet


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
if not IsPostBack then
BindData()
End If
End Sub


Private Sub BindData()
Dim mCommand As SqlCommand
Dim cnSQL as SqlConnection
Dim da as new SqlDataAdapter
try
cnSQL = OpenConnection()
mCommand = New SqlCommand
mCommand.CommandType = CommandType.StoredProcedure
mCommand.Connection = cnSQL
mCommand.CommandText = "Admin_GetPurchaseOrders"
da.SelectCommand = mCommand
da.Fill(dsPO, "POS")
grdPOs.DataSource = dsPO.Tables("POS")
grdPOs.DataBind
cnSQL.Close
'SqlDataPOs.DataBind

'Setup the Programs Listbox.
cnSQL = OpenConnection()
mCommand = New SqlCommand
mCommand.CommandType = CommandType.StoredProcedure
mCommand.Connection = cnSQL
mCommand.CommandText = "Admin_GetPrograms"
da.SelectCommand = mCommand
da.Fill(dsPrograms, "Programs")
cmbPrograms.DataSource = dsPrograms.Tables("Programs")
cmbPrograms.DataTextField = "ProgramName"
cmbPrograms.DataValueField = "ProgramId"
cmbPrograms.DataBind
Catch ex As Exception
msgbox(ex.Message )
Finally
cnSQL.Dispose
mCommand.Dispose
da.Dispose
End Try

End Sub

Private Function OpenConnection() as SqlConnection
try
NetCon = New SqlConnection
With NetCon
.ConnectionString = "packet size=4096;integrated security=SSPI;data
source=Backupsvr\bkupexec;persist security info=False;initial
catalog=PurchaseOrders"
.Open()
End With
return NetCon
Catch ex As Exception
msgbox(ex.Message )
return nothing
End Try
End Function


Protected Sub grdPOs_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdPOs.RowEditing
grdPOs.EditIndex = e.NewEditIndex
DataBind()
End Sub

Protected Sub grdPOs_RowCancelingEdit(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles
grdPOs.RowCancelingEdit
grdPOs.EditIndex = -1
DataBind()
End Sub

Protected Sub grdPOs_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdPOs.RowUpdating
Dim POid as Integer
Dim CatalogId as string, Description as string
Dim Qty as Integer , UnitPrice as Double , Page as Integer
Dim mCommand As SqlCommand
Dim cnSQL as SqlConnection
Dim records(e.NewValues.Count - 1) As DictionaryEntry
Dim entry As DictionaryEntry
Dim DataKeyId as DataKey
try
'Copy the new values into the fields
e.NewValues.CopyTo(records, 0)
For Each entry In records

e.NewValues(entry.Key) = Server.HtmlEncode(entry.Value.ToString())

Next
DataKeyId = grdPOs.DataKeys(e.RowIndex )
'IntId = DataKeyId.Values("ItemId")

POid = DataKeyId("ItemId") 'grdPOs.DataKeys(e.RowIndex).Value
CatalogId = DataKeyId("CatalogId").ToString
'e.NewValues.Item(1).ToString
Description = DataKeyId("ItemDescription").ToString
'CType(e.Item.Cells(2).Controls(0), TextBox).Text
Qty = DataKeyId("Qty") 'CType(e.Item.Cells(3).Controls(0),
TextBox).Text
UnitPrice = DataKeyId("UnitPrice")
'CType(e.Item.Cells(4).Controls(0), TextBox).Text
Page = DataKeyId("Page") 'CType(e.Item.Cells(6).Controls(0),
TextBox).Text
cnSQL = OpenConnection()
mCommand = New SqlCommand
mCommand.CommandType = CommandType.StoredProcedure
mCommand.Connection = cnSQL
mCommand.CommandText = "Admin_UpdatePOItem"
mCommand.Parameters.add("@ItemID", SqlDbType.Int ).Value = POid
mCommand.Parameters.add("@CatalogeId", SqlDbType.Int ).Value = CatalogId
mCommand.Parameters.add("@ItemDescription", SqlDbType.VarChar ,250).Value =
Description
mCommand.Parameters.add("@Qty", SqlDbType.Int).Value = Qty
mCommand.Parameters.add("@UnitPrice", SqlDbType.Money ).Value = UnitPrice
mCommand.Parameters.add("@Page", SqlDbType.int).Direction = Page
mCommand.ExecuteNonQuery
grdPOs.EditIndex = -1
BindData()
Catch ex As Exception
msgbox(ex.Message )
End Try
End Sub


Protected Sub grdPOs_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdPOs.RowDataBound
If e.Row.RowType =DataControlRowType.DataRow then
e.Row.Cells(5).Text = format(convert.ToInt16(e.Row.Cells(3).Text) *
convert.ToDouble(e.row.Cells(4).Text), "0.00")
end if
End Sub
End Class

Does this help any.
Thanks again for the reply.
Michael
 
P

Phillip Williams

Try setting a breakpoint within the grdPOs_RowEditing method and see if it is
invoked.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


Michael said:
Actually, I'm using a dataset to work with the screen. I have setup two
columns to handle the Delete and Edit functions for the grid:
<asp:CommandField ShowEditButton="True" />
<asp:ButtonField CommandName="Delete" Text="Delete" />

Here is the code for the class(Code-Behind) minus a few saving functions to
save some space. Here is the code:
Imports System.Data
imports System.Data.SqlClient


Partial Class EnterPOrder
Inherits System.Web.UI.Page

Private NetCon as SqlConnection
Private dsPO as New DataSet
Private dsPrograms as New DataSet


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
if not IsPostBack then
BindData()
End If
End Sub


Private Sub BindData()
Dim mCommand As SqlCommand
Dim cnSQL as SqlConnection
Dim da as new SqlDataAdapter
try
cnSQL = OpenConnection()
mCommand = New SqlCommand
mCommand.CommandType = CommandType.StoredProcedure
mCommand.Connection = cnSQL
mCommand.CommandText = "Admin_GetPurchaseOrders"
da.SelectCommand = mCommand
da.Fill(dsPO, "POS")
grdPOs.DataSource = dsPO.Tables("POS")
grdPOs.DataBind
cnSQL.Close
'SqlDataPOs.DataBind

'Setup the Programs Listbox.
cnSQL = OpenConnection()
mCommand = New SqlCommand
mCommand.CommandType = CommandType.StoredProcedure
mCommand.Connection = cnSQL
mCommand.CommandText = "Admin_GetPrograms"
da.SelectCommand = mCommand
da.Fill(dsPrograms, "Programs")
cmbPrograms.DataSource = dsPrograms.Tables("Programs")
cmbPrograms.DataTextField = "ProgramName"
cmbPrograms.DataValueField = "ProgramId"
cmbPrograms.DataBind
Catch ex As Exception
msgbox(ex.Message )
Finally
cnSQL.Dispose
mCommand.Dispose
da.Dispose
End Try

End Sub

Private Function OpenConnection() as SqlConnection
try
NetCon = New SqlConnection
With NetCon
.ConnectionString = "packet size=4096;integrated security=SSPI;data
source=Backupsvr\bkupexec;persist security info=False;initial
catalog=PurchaseOrders"
.Open()
End With
return NetCon
Catch ex As Exception
msgbox(ex.Message )
return nothing
End Try
End Function


Protected Sub grdPOs_RowEditing(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewEditEventArgs) Handles grdPOs.RowEditing
grdPOs.EditIndex = e.NewEditIndex
DataBind()
End Sub

Protected Sub grdPOs_RowCancelingEdit(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles
grdPOs.RowCancelingEdit
grdPOs.EditIndex = -1
DataBind()
End Sub

Protected Sub grdPOs_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles grdPOs.RowUpdating
Dim POid as Integer
Dim CatalogId as string, Description as string
Dim Qty as Integer , UnitPrice as Double , Page as Integer
Dim mCommand As SqlCommand
Dim cnSQL as SqlConnection
Dim records(e.NewValues.Count - 1) As DictionaryEntry
Dim entry As DictionaryEntry
Dim DataKeyId as DataKey
try
'Copy the new values into the fields
e.NewValues.CopyTo(records, 0)
For Each entry In records

e.NewValues(entry.Key) = Server.HtmlEncode(entry.Value.ToString())

Next
DataKeyId = grdPOs.DataKeys(e.RowIndex )
'IntId = DataKeyId.Values("ItemId")

POid = DataKeyId("ItemId") 'grdPOs.DataKeys(e.RowIndex).Value
CatalogId = DataKeyId("CatalogId").ToString
'e.NewValues.Item(1).ToString
Description = DataKeyId("ItemDescription").ToString
'CType(e.Item.Cells(2).Controls(0), TextBox).Text
Qty = DataKeyId("Qty") 'CType(e.Item.Cells(3).Controls(0),
TextBox).Text
UnitPrice = DataKeyId("UnitPrice")
'CType(e.Item.Cells(4).Controls(0), TextBox).Text
Page = DataKeyId("Page") 'CType(e.Item.Cells(6).Controls(0),
TextBox).Text
cnSQL = OpenConnection()
mCommand = New SqlCommand
mCommand.CommandType = CommandType.StoredProcedure
mCommand.Connection = cnSQL
mCommand.CommandText = "Admin_UpdatePOItem"
mCommand.Parameters.add("@ItemID", SqlDbType.Int ).Value = POid
mCommand.Parameters.add("@CatalogeId", SqlDbType.Int ).Value = CatalogId
mCommand.Parameters.add("@ItemDescription", SqlDbType.VarChar ,250).Value =
Description
mCommand.Parameters.add("@Qty", SqlDbType.Int).Value = Qty
mCommand.Parameters.add("@UnitPrice", SqlDbType.Money ).Value = UnitPrice
mCommand.Parameters.add("@Page", SqlDbType.int).Direction = Page
mCommand.ExecuteNonQuery
grdPOs.EditIndex = -1
BindData()
Catch ex As Exception
msgbox(ex.Message )
End Try
End Sub


Protected Sub grdPOs_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdPOs.RowDataBound
If e.Row.RowType =DataControlRowType.DataRow then
e.Row.Cells(5).Text = format(convert.ToInt16(e.Row.Cells(3).Text) *
convert.ToDouble(e.row.Cells(4).Text), "0.00")
end if
End Sub
End Class

Does this help any.
Thanks again for the reply.
Michael
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top