DataGrid - SelectedRow

Last post 10-16-2005 6:53 PM by Girlnet. 3 replies.

Sort Posts:

  • DataGrid - SelectedRow

    10-06-2005, 9:58 PM
    • Participant
      768 point Participant
    • Girlnet
    • Member since 09-09-2003, 6:02 AM
    • Canton, Michigan
    • Posts 191
    In my conversion quest, I'm trying to get the ItemIndex.  Normally, I would write:

    <code>  Sample code I took from another app

    Sub doInsert(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

    If e.CommandName = "Delete" Then
    Dim conn As New SqlConnection(ConfigurationSettings.AppSettings("connString"))
    Dim objDR As SqlDataReader
    Dim Cmd As New SqlCommand("TAM_DEL_ITEMS", conn)
    Cmd.CommandType = CommandType.StoredProcedure
    Cmd.Parameters.Add(
    New SqlParameter("@ID", e.Item.ItemIndex))
    Cmd.Parameters("@ID").Value = DataGrid1.DataKeys(
    CType(e.Item.ItemIndex, Integer))
    conn.Open()
    Cmd.ExecuteNonQuery()
    DataGrid1.EditItemIndex = -1
    conn.Close()
    End If

    BindData()

    End Sub
    </code>

    Here's what I've tried . . .

    <code>

    Sub doInsertEmail(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
    If e.CommandName = "DeleteEmail" Then
    Dim objFiles As New PartyPlannerController
    Dim EmailID As Integer = CType(DataGrid1.DataKeys(e.Item.ItemIndex), Integer
    Dim objFile As PartyPlannerInfo = objFiles.DeleteEmails(EmailID))<-------------------------  Get item does not produce value error.
    End If
    End Sub
    </code>

    I've tried a few different ways to do this and just not licking it.  Plus, I'm not seeing anything in the posts.  Is this too simple? Is everyone aceing this?  Can anyone point out what I'm doing wrong?

    Any assistance would be appreciated!

  • Re: DataGrid - SelectedRow

    10-07-2005, 10:58 AM
    • Member
      480 point Member
    • GOHDS
    • Member since 07-24-2005, 4:37 AM
    • Posts 97

    Here's the code i use, i usually stick the primaryid on a hidden cell on the datagrid, in this sample is cell 1
    then when the edit or update key is hit, i just retrieve the value from the cell,
     
    make sure you rebind your datasource on postback, that maybe the problem your are having

    ------------------------------------------
    Private
    Sub mydg_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles mydg.EditCommand

    dim myid as string  = e.Item.Cells(1).Text

    >>>' do something

    End Sub


    ---------------------------

    Hope this helps

    ray

  • Re: DataGrid - SelectedRow

    10-07-2005, 11:02 AM
    • Participant
      1,610 point Participant
    • jjohns09
    • Member since 06-22-2002, 5:58 PM
    • Overland Park, Kansas
    • Posts 320

    Everything appears correct as long as you are assigning your DataKeyField property in your datagrid to your ID column.

    Jeff Johnson

    http://www.bestmodules.com - Modules, Skins & Skin Objects
  • Re: DataGrid - SelectedRow

    10-16-2005, 6:53 PM
    • Participant
      768 point Participant
    • Girlnet
    • Member since 09-09-2003, 6:02 AM
    • Canton, Michigan
    • Posts 191
    Just wanted to post a follow up . . . this worked for me

    <code>

    Sub doInsertEmail(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles Datagrid2.ItemCommand

    If e.CommandName = "DeleteEmail" Then

    Dim objFiles As New PartyPlannerController

    Dim EmailiD As Integer

    EmailiD = CType(Datagrid2.DataKeys(e.Item.ItemIndex), Integer)

    objFiles.DeleteEmails(EmailiD)

    BindEmails()

    End If

    End Sub
    </code>

Page 1 of 1 (4 items)