Page 1 of 1

Getting Value of particular key in gridview datakeys

Posted: Fri Feb 06, 2009 12:36 pm
by jmestep
I'm adapting the EditSimilarProducts.ascx to use to link webpages of a custom type to a product.
The SearchResultsGrid now has three data keys, CategoryId, CatalogNodeId, CatalogNodeTypeId.

The RelatedProductGrid has 4 keys, objectId1, objecctType1, objectId2, objectType2

When I add/remove a link, the objectId1 is the parent productId and the objectType1 is 1 since it's a product.
I need to map the CatalogNodeId to the objectId2 and the CatalogNodeTypeId to the objectType2.
I'm changing the following:

Code: Select all

 protected void RelatedProductGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int relatedProductId = (int)e.Keys[0];
        SetLink(relatedProductId, false);
        //CHECK THE SEARCH RESULTS GRID TO SEE IF THIS ITEMS APPEARS
        int tempIndex = 0;
        foreach (DataKey key in SearchResultsGrid.DataKeys)
        {
            int tempId = (int)key.Value;
            if (relatedProductId == tempId)
to

Code: Select all

protected void RelatedObjectGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
       int relatedObjectId = (int)e.Keys[2];
       int relatedType = (int)e.Keys[3];
       SetLink(relatedObjectId,relatedType, false);
        //CHECK THE SEARCH RESULTS GRID TO SEE IF THIS ITEMS APPEARS
        int tempIndex = 0;
          int tempId = (int)SearchResultsGrid.DataKeys[1].Value;
         int tempType = (byte)SearchResultsGrid.DataKeys[2].Value;
        
but something is not picking up for the keys for tempId and tempType. The code keeps picking up the CategoryId and saving that to the objectId2 in the database with 0 in the objectType2 field.
Can anyone tell me how to edit the above? I can change the order of the DataKeyNames="CategoryId,CatalogNodeId,CatalogNodeTypeId" to put the CatalogNodeId first and then that gets put into the database OK, but the type doesn't get put in.
Thanks

Re: Getting Value of particular key in gridview datakeys

Posted: Mon Feb 09, 2009 6:42 am
by mazhar
Instead of using the below code

Code: Select all

  int tempId = (int)SearchResultsGrid.DataKeys[1].Value;
         int tempType = (byte)SearchResultsGrid.DataKeys[2].Value;
Give a try to the following

Code: Select all

int tempId = (int)SearchResultsGrid.DataKeys[data item index].Value;
int tempType = (byte)SearchResultsGrid.DataKeys[data item index].Value[1];
where data item index is the record index in grid view.