none
Bind List<Object> to GridView RRS feed

  • Question

  • First of all, I refereed the following threads but could not figure out the solution, so I decided to post a new thread :

    I am using ASP.NET 4.0

    I've a small class as below :

    public class Attachment { public int docId; public int modifiedUserId; public string docPath; public string docNameWoExtension; public string docExtension; public DateTime CreatedDate; public DateTime ModifiedDate; };

    //I do not have getters and setters

    and the query below

    List<Attachment> AttachmentsByID = 
    ((
        from doc in CommonDb.DOCUMENTS
        where 
        ((doc.id1.HasValue && doc.id1 == thisID) ||(doc.id2.HasValue && doc.id2 == thisID))
        select new Attachment
        {
            docId = doc.DOCID,
            docPath = doc.DOCPATH,
            docNameWoExtension = doc.DOCNAME,
            docExtension = doc.Extension,
            modifiedUserId = doc.ModifiedUserID,
            ModifiedDate = doc.ModifiedDate,
            CreatedDate = doc.CreatedDate
        }
    ) as Queryable<Attachment>).ToList<Attachment>();

    which gives me list of objects I get 4 records of type Attachment but, to bind this data to GridView gvAttachments if I do:

    gvAttachments.DataSource =  AttachmentsByID;

    gvAttachments.DataBind();

    ... I get an error :

    The data source for GridView with id 'gvAttachments' did not have any properties or attributes from which to generate columns.  Ensure that your data source has content.

    Definitely following code works but it does not make any sense : (I did that just to make sure that there is no other problem)

    DataColumn[] columns = new DataColumn[]
    		{
                        new DataColumn("docId"),
                        new DataColumn("docPath"),
                        new DataColumn("docNameWoExtension"),
                        new DataColumn("docExtension"),
                        new DataColumn("modifiedUserId"),
                        new DataColumn("ModifiedDate"),
                        new DataColumn("CreatedDate")
                	};
                
    DataTable tblAttachments = new DataTable();
    tblAttachments.Columns.AddRange(columns);
    
    CommonEntities CommonDb = new CommonEntities();
    List<Attachment> AttachmentsByID = 
    ((
        from doc in CommonDb.DOCUMENTS
        where 
        ((doc.id1.HasValue && doc.id1 == thisID) ||(doc.id2.HasValue && doc.id2 == thisID))
        select new Attachment
        {
            docId = doc.DOCID,
            docPath = doc.DOCPATH,
            docNameWoExtension = doc.DOCNAME,
            docExtension = doc.Extension,
            modifiedUserId = doc.ModifiedUserID,
            ModifiedDate = doc.ModifiedDate,
            CreatedDate = doc.CreatedDate
        }
    ) as Queryable<Attachment>).ToList<Attachment>();
    
    DataRow dr;
    foreach (var item in AttachmentsByID)
    {
        dr = tblAttachments.NewRow();
        dr["docId"] = item.docId;
        dr["docPath"] = item.docPath;
        dr["docNameWoExtension"] = item.docNameWoExtension;
        dr["docExtension"] = item.docExtension;
        dr["modifiedUserId"] = item.modifiedUserId;
        dr["ModifiedDate"] = item.ModifiedDate;
        dr["CreatedDate"] = item.CreatedDate;
    
        tblAttachments.Rows.Add(dr);
    }
    gvAttachments.DataSource = tblAttachments;
    gvAttachments.DataBind();



    Monday, June 24, 2013 9:09 PM

Answers

  • Declare your class like this and it will work.

    public class Attachment
    {
        public int docId { set; get; }
        public int modifiedUserId { set; get; }
        public string docPath { set; get; }
        public string docNameWoExtension { set; get; }
        public string docExtension { set; get; }
        public DateTime CreatedDate { set; get; }
        public DateTime ModifiedDate { set; get; }
    };


    Please Mark as Answer and Vote as Helpful if I helped.

    Also please visit my blog http://msguy.me/


    Tuesday, June 25, 2013 6:57 AM