Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

asp.net 4.0 gridview delete a record, trouble with data type conversions

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
trying to get the row Id and then the REcord ID to delete a record form the gridview. I am trying to pass the Row Id to a variable the trying to get eth value of the Record ID out of a cell called "ID"

Code:
    Protected Sub GridView2_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView2.RowDeleting

        Dim RetVal As String

        RetVal = MsgBox("Are you sure you want to delete record?", vbInformation + vbYesNoCancel, "Confirm Delete Record")

        If RetVal = vbYes Then
            Dim RowID As Object = GridView2.Rows(e.RowIndex)
            Session("EditSQLRecordID") = GridView2.Rows(RowID).FindControl("ID")

            'delete record calling the function which calls the stored procedure
            sp_SOWDeleteOneTimeReportingRecord(Session("EditSQLRecordID"))

            Me.GridView2.DataBind()
        End If
    End Sub

DougP
 
Set the DataKeys property on your gridview to whatever column is your ID.
Then in your RowDeleting event do something like this:
Code:
   Dim RowID as string =  e.Keys("YourDataKeyNameGoesHere")

Also, as a side note, do not use a MsgBox. This will only work locally, because your dev machine is the server. Once you roll that code to production, or a staging site, it will run, but you will never see the popup.
 
Ok whats is "YourDataKeyNameGoesHere" I tried "ID" but returns "Nothing". I am populating the grid from scratch it's unbound but the column name is ID. it has the Unique Record ID and
if I could just get it I would be a happy camper. no idea why this is so clumsy in .NET? one methosd you can use something and the other method you can't.
I know about message box but can't make anything work with that either. you got a snippet for me to pop up a message?

I know enough .NET to just get by. been doing VB6 and VBA which is much more forgiving than .NET.

DougP
 
Ok I got it
I was looking on MSDN at datakeyname and added the RED line and it
WORKS !!!!!!!! omg its works after 5 hours of head banging ####

<asp:GridView ID="GridView2" runat="server" BackColor="#CCCCCC" AllowSorting="True"
OnRowDeleting="GridView2_RowDeleting"
DataKeyNames="ID"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4"
CellSpacing="2" ForeColor="Black">
....

DougP
 
Glad you got it. Sorry my explanation wasn't clear enough about setting the DatakeyName property.
As for a pop up, you need to do it on the client side with javascript.
You can use alert('you message');
OR
If you want a return back, you can use confirm:
confirm('are you sure you want to delete this row?');

That will return true or false.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top