This is a migrated thread and some comments may be shown as answers.

Get Datakey value for Heirarchial Radgrid

1 Answer 514 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 10 Jun 2014, 03:47 PM
I am able to get the datakeyvalue for the 1st level of the radgrid, but now trying to get the datakey value fo rthe second level of the radgrid to do an insert at the 3rd level of the radgrid.  I used the same statement for the 3rd level but it always returns 0, so I know its not working.  I tried putting in parent.parent but nothing seems to work for me.

It is this peice in the 3rd level insert
GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
          int PosId = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["intpositionId"]);

// all the Insert Commands for the Grids in order of Grid Heirarchy
     //Insert command for the Sections  1st level
     if (e.CommandName == RadGrid.PerformInsertCommandName && e.Item.OwnerTableView.Name == "Sections")
     {
         GridEditableItem item = e.Item as GridEditableItem;
         TextBox section = (TextBox)item.FindControl("txtSection");
 
         sql = "Execute usp_InsertSection '" + c.SanitizeString(section.Text) + "', " + c.GetUserId();
 
         c.InsertUpdateDelete(sql);
         myradGrid.Rebind();
     }
 
     //Insert command for positions  2nd Level
     if (e.CommandName == RadGrid.PerformInsertCommandName && e.Item.OwnerTableView.Name == "Positions")
     {
         GridEditableItem item = e.Item as GridEditableItem;
         GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
         //int sectionId = Convert.ToInt32(item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["intSectionId"]);
         int secId = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["intSectionId"]);
         TextBox descrip = (TextBox)item.FindControl("txtdescription");
         TextBox scale = (TextBox)item.FindControl("txtScale");
         TextBox posType = (TextBox)item.FindControl("txtPosType");
         TextBox grade = (TextBox)item.FindControl("txtGrade");
 
         sql = "Execute usp_InsertPosition " + secId + ", '" + c.SanitizeString(descrip.Text) + "', '" + c.SanitizeString(scale.Text) + "', '" + c.SanitizeString(posType.Text) + "', '" + c.SanitizeString(grade.Text) + "'";
 
         c.InsertUpdateDelete(sql);
     }
 
     //Insert for Personnel added to the Position  3rd Level
     if (e.CommandName == RadGrid.PerformInsertCommandName && e.Item.OwnerTableView.Name == "Personnel")
     {
         GridEditableItem item = e.Item as GridEditableItem;
         GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
         int PosId = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["intpositionId"]);
         CheckBox backfill = (CheckBox)item.FindControl("cbBackFill");
         CheckBox manager = (CheckBox)item.FindControl("cbManager");
         CheckBox assistManager = (CheckBox)item.FindControl("cbAssistMan");
         int backfillck = 0;
         int managerck = 0;
         int assManagerck = 0;
 
         if (backfill.Checked == true)
         {
             backfillck = 1;
         }
 
         if (manager.Checked == true)
         {
             managerck = 1;
         }
 
         if (assistManager.Checked == true)
         {
             assManagerck = 1;
         }
         
         sql = "Execute usp_InsertPersonnel " + PosId + ", " + HFAdminId.Value + ", " + backfillck + ", " + managerck + ", " + assManagerck + "";
         Response.Write(sql);
         Response.End();
         c.InsertUpdateDelete(sql);
     }




1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 11 Jun 2014, 06:23 AM
Hi Kevin,

You can identify each table using its Name property and access the Parent using e.Item.OwnerTableView.ParentItem. Make sure you have added the correct DataKeyNames. Please take a look at the sample code I have a main grid, Country and State table inside that and District inside State.

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
 if (e.CommandName == RadGrid.PerformInsertCommandName)
 {
  GridEditableItem editItem = e.Item as GridEditableItem;
  GridDataItem parentItem = e.Item.OwnerTableView.ParentItem;
  if (e.Item.OwnerTableView.Name == "Country")
    {
     //Code to insert to main grid
    }
    else if (e.Item.OwnerTableView.Name == "State")
    {
    //Access parent datakey value
    int CountryID = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["CountryID"]);
    //Code to insert to 2nd level table
    }
    else if (e.Item.OwnerTableView.Name == "District")
    {
    //Access parent datakey value
    int StateID = Convert.ToInt32(parentItem.OwnerTableView.DataKeyValues[parentItem.ItemIndex]["StateID"]);
    //Code to insert to 3rd level table
    }
  }
}

Thanks,
Shinu
Tags
Grid
Asked by
Kevin
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or