如何将对象类的子对象绑定到ASP.NET中的GridView?

时间:2013-08-02 09:29:09

标签: asp.net oop gridview collections

我正在为项目构建客户服务模块。我将用户提交的所有投诉存储在数据库中,并将其提取到管理界面上显示。我的两个实体课程代表投诉详情和投诉提供者详情如下

    public class Complaint
    {
        #region Fields of the class
        /// <summary>
        /// List of Complaints
        /// </summary>
        private List<Complaint> complaints = new List<Complaint>();

        #endregion

        #region Properties of the class

        public int Id { get; set; }
        public string Type { get; set; }
        public string Description { get; set; }
        public string ActionTaken { get; set; }
        public bool Status { get; set; }
        public DateTime Date { get; set; }
        public CSRInfo CsrId { get; set; }
        public ComplaintRaiserInfo complaintraiser { get; set; }

        #endregion

        #region Methods of the class

        /// <summary>
        /// Gets all the complaints from the List of complaints
        /// </summary>
        /// <returns>Returns the list of the complaints</returns>
        public List<Complaint> GetComplaints()
        {
            return complaints;
        }

        /// <summary>
        /// Adds a complaint to List of Complaints
        /// </summary>
        /// <param name="newComplaint"></param>
        public void AddComplaint(Complaint newComplaint)
        {
            complaints.Add(newComplaint);
        }

        #endregion

    }

    public class ComplaintRaiserInfo
    {
        #region Fields of the class

        public List<int> ComplaintIds = new List<int>();

        #endregion


        #region Properties of the class

        public FlightBooking BookingId { get; set; }
        public string Name { get; set; }
        public string MobileNo { get; set; }
        public string EmailId { get; set; }

        #endregion

        #region Methods of the class
        /// <summary>
        /// Gets all the complaint ids from the List of complaint Ids
        /// </summary>
        /// <returns>Returns the list of the complaint ids</returns>
        public List<int> GetComplaintIds()
        {
            return ComplaintIds;
        }

        /// <summary>
        /// Adds a complaint id to List of Complaint Ids
        /// </summary>
        /// <param name="complaintId"></param>
        public void AddComplaint(int complaintId)
        {
            ComplaintIds.Add(complaintId);
        }
        #endregion

    }

数据库中的数据已成功获取,我将其绑定到ASP.net网页中的GridView,如下所示,

    ComplaintManager complaintManager = new ComplaintManager();
    List<Complaint> complaints = new List<Complaint>();
    complaints = complaintManager.GetComplaints();

    GridView1.DataSource = complaints;
    GridView1.DataBind();

我的问题是显示了Complaint类的属性,但不显示ComplaintRaiserInfo类的属性。请帮我解决问题。

1 个答案:

答案 0 :(得分:0)

您可以轻松使用LINQ来压缩表达式,这通常是绑定到gridview的问题:

var complaints = complaintManager.GetComplaints().Select((i) => new { 
  i.ComplaintID,
  i.ComplaintRaiser.ComplaintRaiserID,
  .
  .
});

这很好地与网格绑定。您可以在标记中尝试点符号,如在ComplainRaiser.ComplaintRaiserID中对带有eval的模板化字段(我自己没有成功使用BoundField ...)