Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2320
Help! "ArgumentException: An item with the same key has already been added"
posted

I can't get my child band to work properly with my data structure.  Here is my data

public class MessageHelper
    {
        public int MessageId { getset; }
        public string Message { getset; }
        public DateTime Date { getset; }

        public List<AttachmentHelper> Attachments { getset; }

        public MessageHelper()
        {
            Attachments = new List<AttachmentHelper>();
        }
    }
    
    public class AttachmentHelper
    {
        public int AttachmentId { getset; }
        public int MessageId { getset; }
        public DateTime AttachmentDate { getset; }
        public string FileName { getset; }        
    }

I create 2 rows in a function that returns a List<MessageHelper>

List<MessageHelper> messages = new List<MessageHelper>();

            messages.Add(new MessageHelper()
            {
                MessageId = 1,
                Date = DateTime.Now,
                Message = "This is a fake message.",
                Attachments = new List<AttachmentHelper>()
                {
                    new AttachmentHelper() { AttachmentId = 1, MessageId = 1, FileName = "MyTextFile.txt", AttachmentDate = DateTime.Now },
                    new AttachmentHelper() { AttachmentId = 2, MessageId = 1, FileName = "MyTextFile2.txt", AttachmentDate = DateTime.Now }
                }
            });

            messages.Add(new MessageHelper()
            {
                MessageId = 2,
                Date = DateTime.Now,
                Message = "This is another fake message.",
                Attachments = new List<AttachmentHelper>()
                {
                    new AttachmentHelper() { AttachmentId = 3, MessageId = 2, FileName = "Test.doc", AttachmentDate = DateTime.Now },
                    new AttachmentHelper() { AttachmentId = 4, MessageId = 2, FileName = "Test2.doc", AttachmentDate = DateTime.Now },
                    new AttachmentHelper() { AttachmentId = 5, MessageId = 2, FileName = "Test3.doc", AttachmentDate = DateTime.Now }
                }
            });
that collection is assigned to the datasource

here is the grid code
<ig:WebHierarchicalDataGrid ID="grid" runat="server" AutoGenerateColumns="false" AutoGenerateBands="false" 
 InitialDataBindDepth="0" DataKeyFields="MessageId" Height="300" Width="800px">  
 <AjaxIndicator Enabled="False" />              
 <Columns>  
  <ig:BoundDataField DataFieldName="MessageId" Key="MessageId" Header-Text="ID" Width="30px" />  
   <ig:BoundDataField DataFieldName="Message" Key="Message" Header-Text="Message"  />  
   <ig:BoundDataField DataFieldName="Date" Key="Date" Header-Text="Date" Width="100px" />                  
 </Columns>  
 <Bands>  
  <ig:Band Key="Attachments" AutoGenerateColumns="false" DataKeyFields="AttachmentId" >  
   <Columns>  
   <ig:BoundDataField DataFieldName="AttachmentId" Key="AttachmentId" Header-Text="Attachment ID" /> 
    <ig:BoundDataField DataFieldName="FileName" Key="FileName" Header-Text="Attached File" />  
     <ig:BoundDataField DataFieldName="AttachmentDate" Key="AttachmentDate" Header-Text="Last Modified" />                            
   </Columns> 
</ig:Band>  
</Bands> 
</ig:WebHierarchicalDataGrid>

If I set AutoGenerateBands to false, it works fine.
Why is my band column definition not working?

Any help would be appreciated.

  • 2320
    Verified Answer
    posted

    Figured this one out.  My childband was missing the DataMember="Attachments" property.  I was actually going to leave AutoGenerateBands=true and in the InitializeRow event of the grid, adjust my columns layout there (and it worked, but was a hack).  While searching for a different error I came across this post

    http://community.infragistics.com/forums/t/35449.aspx

    Which helped.