Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok,

when I am adding a new row to the nested gridview(gvBulletItems) and there are existing rows I can get parent ID with

gvBulletItems.DataKeys[0].Value

(BTW I hate magic numbers)

however when I a have parent gridview with no Items in the nested gridview DataKeys comes back as null

I have defined my EmptyDataTemplate thusly:
 <EmptyDataTemplate>
  There are no Bullets for this bullet group. 
  <table id="tblnewbullets"  class="tbl-newItems" runat="server">
      <tr class="tbl-newItems">
         <td class="tbl-newItems" > List Order</td>
         <td> Display Text</td>
      </tr>
      <tr>
          <td>
             <asp:TextBox ID="txtbxNewListOrder" runat="server"></asp:TextBox>
          </td>
          <td>
             <asp:TextBox ID="txtbxNewDisplayText" runat="server"></asp:TextBox>
          </td>
          <td>
             <asp:LinkButton ID="linkAddBullet" CommandName="AddNewBullet" runat="server">Add</asp:LinkButton>
          </td>
       </tr>
    </table>
</EmptyDataTemplate>


and this defined before

<Columns>
.....
</Columns>


One of the things I thought of was that I could it from is the ID of the div that the nested gridview as its ID is defined as
"div<%# Eval("BulletGroupID") %>"

but when I look that is not the ID.

here is the code that binds both gridviews

if (row.RowType == DataControlRowType.DataRow)
     {
        InfoCenterParent icParent = (InfoCenterParent)Session[PARENT];
        List<InfoCenterBulletGroup> bulletGroupsData = icParent.bulletGroups;
        Label lblGroupBulletID = (Label)row.FindControl("lblBulletGroupID");
        int BulletGroupID = int.Parse(lblGroupBulletID.Text);
        InfoCenterBulletGroup bg = bulletGroupsData.Single(b => b.BulletGroupID == BulletGroupID);
        GridView gvBulletItems = new GridView();
        gvBulletItems = (GridView)row.FindControl("gvBulletItems");
        gvBulletItems.DataSource = bg.bulletItems;
        gvBulletItems.DataBind();
     }


and here is how I am retrieving the BulletgroupID in the OnRowCommand of the nested gridview
gvBulletItems.DataKeys[0].Value.ToString()

this object(gvBulletItems.DataKeys) is not null if I am added a bulletitem to a gridview that has existing items
however if I am adding to a the nested gridview that has no items then it comeback as null

Here is the object that gets bound to the gridviews
public class InfoCenterBulletGroup  
    {  
        public int BulletGroupID { get; set; }
        public int InfoCenterID { get; set; }
        public string DisplayText { get; set; }
        public int ListOrder { get; set; }
        public List<InfoCenterBulletItem> bulletItems { get; set; }
        public string LastUpdatedBy { get; set; }
        public DateTime LastUpdatedDate { get; set; }
    }
    public class InfoCenterBulletItem
    {
        public int BulletID { get; set; }
        public int BulletGroupID { get; set; }
        public int ListOrder { get; set; }
        public string DisplayText { get; set; }
        public string LastUpdatedBy { get; set; }
        public DateTime LastUpdatedDate { get; set; }
    }


What I have tried:

I have tried assigning
<%# Eval("BulletGroupID") %>"

to label text in my emptyDataTemplate

I have dug through all the properties of both gridviews
I have worn out my keyboard with Google searches
Posted
Updated 17-Dec-18 4:57am
v2

I'm not exactly sure what exactly is your goal. A GridView row doesn't have an ID however, you can reference the row by casting the sender of the object that triggers the event. For example, in your Add Button, you can something like this:

C#
protected void LinkButton1_Click(object sender, EventArgs e){
        LinkButton lb = (LinkButton)sender;
        GridViewRow row = (GridViewRow)lb.NamingContainer;

        //here the row variable is the row of your parent GridView

}
 
Share this answer
 
Comments
RmcbainTheThird 15-Dec-18 6:56am    
True the row does not have an ID but it does have columns and what I need to do is get the value of one of those columns when adding a child row We have a one to many relationship between the data in the parent/top gridview and the child/nested gridview. When the parent row has one or more child records(the nested gridview) and I want to add addition rows/records I can get the value of the column that defines that relationship by getting the value of gvBulletItems.DataKeys[0].Value. gvBulletItems being the child gridview and the sender parm on the RowComandEvent. However if there are no rows in the childgridview then for whatever reason gvBulletItems.DataKeys[0] is null. I need to get the data stored in one of the columns (the unique ID)of parent row that defines the relationship between the parent gridview and the new row I am adding to the child gridview.
Vincent Maverick Durano 16-Dec-18 22:02pm    
Can you please update your question and include the code binding your GridView and how are you accessing the DataKey at RowCommand?
Ok it is a bit of a hack, but my solution was to add an item the db table that populates the nested grid gridview when I am creating a record in the db table that populates the parent grid view.

InfoCenterBulletGroup bg = new InfoCenterBulletGroup();
     int.TryParse(txtbxListOrder.Text, out tmp);
     bg.DisplayText = txtbxDisplayText.Text;
     bg.ListOrder = tmp;
     bg.LastUpdatedBy = _nbid;
     bg.InfoCenterID = int.Parse(hParentID.Value);
     InfoCenterData sql = new InfoCenterData(_connString);
     int bulletGroupID = sql.AddBulletGroup(bg);
     InfoCenterBulletItem bulletItem = new InfoCenterBulletItem();
     bulletItem.BulletGroupID = bulletGroupID;
     bulletItem.DisplayText = "New Bullet";
     bulletItem.ListOrder = 0;
     bulletItem.LastUpdatedBy = _nbid;
     sql.AddBulletItem(bulletItem);
     UpdateParentObject();
     InfoCenterParent icParent = (InfoCenterParent) Session[PARENT];
     List<InfoCenterBulletGroup> bulletGroupsData = icParent.bulletGroups;
     gvBulletGroup.DataSource = bulletGroupsData;
     gvBulletGroup.DataBind();
     txtbxListOrder.Text = string.Empty;
     txtbxDisplayText.Text = string.Empty;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900