DataKeys in Datagrid

Discussion in 'ASP .Net Datagrid Control' started by Atif Jalal, Jul 21, 2003.

  1. Atif Jalal

    Atif Jalal Guest

    I have a datagrid with a checkbox as one of the column. When the
    checkboxes are selected and a delete button at the bottom of the grid
    is clicked, I want the selected row in grid to be deleted. I am having
    trouble in determining the key of the selected record. I get the error
    index out of range. The code is as follows in VB.net. Your help
    appreciated.

    For Each dgi In MySearchesGrid.Items
    cb = CType(dgi.Cells(0).Controls(1), CheckBox)
    If cb.Checked Then
    '' Determine the key of the selected record ...
    bookid = CType(MySearchesGrid.DataKeys(i), Integer)
    <----- get an error at this point
    ' ... get a pointer to the corresponding dataset
    record ...
    dr = Me.dsMySearches.Tables(0).Rows(bookid)
    ' ... and delete it.
    dr.Delete()
    End If
    i += 1
    Next
     
    Atif Jalal, Jul 21, 2003
    #1
    1. Advertising

  2. Atif Jalal

    Saravana Guest

    Instead of using datakeyfield to get the primary key value, you are
    accessing datakey.

    So Instead of this statement
    bookid = CType(MySearchesGrid.DataKeys(i), Integer)

    Use this statement in your code, this should work.

    bookid = CType(MySearchesGrid.DataKeyField(i),Integer)

    --
    Saravana
    Microsoft India Community Star,
    MCAD,SE,SD,DBA.


    "Atif Jalal" <> wrote in message
    news:...
    > I have a datagrid with a checkbox as one of the column. When the
    > checkboxes are selected and a delete button at the bottom of the grid
    > is clicked, I want the selected row in grid to be deleted. I am having
    > trouble in determining the key of the selected record. I get the error
    > index out of range. The code is as follows in VB.net. Your help
    > appreciated.
    >
    > For Each dgi In MySearchesGrid.Items
    > cb = CType(dgi.Cells(0).Controls(1), CheckBox)
    > If cb.Checked Then
    > '' Determine the key of the selected record ...
    > bookid = CType(MySearchesGrid.DataKeys(i), Integer)
    > <----- get an error at this point
    > ' ... get a pointer to the corresponding dataset
    > record ...
    > dr = Me.dsMySearches.Tables(0).Rows(bookid)
    > ' ... and delete it.
    > dr.Delete()
    > End If
    > i += 1
    > Next
     
    Saravana, Jul 22, 2003
    #2
    1. Advertising

  3. Atif Jalal

    Prasad Guest

    Hi

    Instead of use MySearchesGrid.DataKeys(i) try using
    MySearchesGrid.DataKeys(dgi.ItemIndex)

    Prasad
    HTH

    "Atif Jalal" <> wrote in message
    news:...
    > I have a datagrid with a checkbox as one of the column. When the
    > checkboxes are selected and a delete button at the bottom of the grid
    > is clicked, I want the selected row in grid to be deleted. I am having
    > trouble in determining the key of the selected record. I get the error
    > index out of range. The code is as follows in VB.net. Your help
    > appreciated.
    >
    > For Each dgi In MySearchesGrid.Items
    > cb = CType(dgi.Cells(0).Controls(1), CheckBox)
    > If cb.Checked Then
    > '' Determine the key of the selected record ...
    > bookid = CType(MySearchesGrid.DataKeys(i), Integer)
    > <----- get an error at this point
    > ' ... get a pointer to the corresponding dataset
    > record ...
    > dr = Me.dsMySearches.Tables(0).Rows(bookid)
    > ' ... and delete it.
    > dr.Delete()
    > End If
    > i += 1
    > Next
     
    Prasad, Jul 22, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marina

    DataList.DataKeys problem

    Marina, Jul 17, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    1,833
    the Dude
    Jul 17, 2003
  2. Alex
    Replies:
    0
    Views:
    386
  3. Dave

    BaseDataList.DataKeys

    Dave, Oct 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    674
    =?Utf-8?B?VGFsZXNwaW5uZXI=?=
    Nov 19, 2004
  4. =?Utf-8?B?TmV2aWxsZVQ=?=

    datakeys error

    =?Utf-8?B?TmV2aWxsZVQ=?=, Apr 27, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    465
    =?Utf-8?B?TmV2aWxsZVQ=?=
    Apr 27, 2005
  5. Earl Teigrob

    Using Datagrid with EnableViewState="false" gets wrong DataKeys value

    Earl Teigrob, Jul 17, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    270
    Earl Teigrob
    Jul 17, 2003
Loading...

Share This Page