2017-12-05 3 views
0

클래스 학생에게 DataGridView에 또 다른 클래스에서어떻게 형태로

class StudentInfo 
    { 
     public int StudentId { get; set; } 
     public String FullName { get; set; } 
     public String LessonStudy { get; set; } 
     public String GroupName { get; set; } 
     public StatusStudentBell StatusStudentBell { get; set; } 
    } 

클래스 StatusStudentBell

class StatusStudentBell 
    { 
     public String Bell1 { get; set; } 
     public String Bell2 { get; set; } 
     public String Bell3 { get; set; } 
     public String Bell4 { get; set; } 
    } 

을 클래스 목록을 연결하는

ConservatoryEntities ConservatoryEntities = new ConservatoryEntities(); 
     private void Form1_Load(object sender, EventArgs e) 
     { 
      var student = ConservatoryEntities.LocationTimeClasses.Where(l => 
        l.Day.Name.Equals("Friday")); 

      var dateNow = DateTime.Parse(DateTime.UtcNow.ToShortDateString()); 

      var studentInfo = new List<StudentInfo>(); 
      foreach (var item in student) 
      { 

       studentInfo.AddRange(item.LessonToFieldStudy.FieldOfStudy.Students.ToList().Select(s => 
       new StudentInfo() 
       { 
        FullName = s.Name, 
        GroupName = s.Team.Name, 
        LessonStudy = s.FieldOfStudy.Name, 
        StatusStudentBell = new StatusStudentBell() 
        { 
         Bell1 = item.LessonToFieldStudy.RollCalls.FirstOrDefault(r => r.fk_bell_Id == 1 && 
         r.Date == dateNow)?.Absent + "", 
         Bell2 = item.LessonToFieldStudy.RollCalls.FirstOrDefault(r => r.fk_bell_Id == 2 && 
         r.Date == dateNow)?.Absent + "", 
        } 
       }).ToList()); 


      } 

     } 

저는 C#을 사용하고 있으며 초보자입니다.

C#에서는 내가 만든이 목록을 dataGridView에 연결하려고합니다.

StatusStudentBell 클래스가 DataGridView에 연결할 수있는 StudentInfo 목록을 어떻게 추가 할 수 있습니까?

도와주세요.

다음
+0

확인 : https://stackoverflow.com/questions/6473326/using-a-list-as-a-data-source-for-datagridview 또한 조회하려고 조금 더. – JCM

답변

0

시도 :이

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication7 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 

      DataTable dt = new DataTable(); 

      dt.Columns.Add("StudentId", typeof(string)); 
      dt.Columns.Add("FullName", typeof(string)); 
      dt.Columns.Add("LessonStudy", typeof(string)); 
      dt.Columns.Add("GroupName", typeof(string)); 
      dt.Columns.Add("Bell1", typeof(string)); 
      dt.Columns.Add("Bell2", typeof(string)); 
      dt.Columns.Add("Bell3", typeof(string)); 
      dt.Columns.Add("Bell4", typeof(string)); 

      List<StudentInfo> studentInfo = new List<StudentInfo>() { 
       new StudentInfo() { FullName = "John", GroupName = "Apple", LessonStudy = "Math", StudentId = 123, StatusStudentBell = new StatusStudentBell() { Bell1 = "A", Bell2 = "B", Bell3 = "C", Bell4 = "D"}}, 
       new StudentInfo() { FullName = "Marry", GroupName = "Bannana", LessonStudy = "English", StudentId = 345, StatusStudentBell = new StatusStudentBell() { Bell1 = "E", Bell2 = "F", Bell3 = "G", Bell4 = "H"}}, 
       new StudentInfo() { FullName = "Harry", GroupName = "Grape", LessonStudy = "French", StudentId = 678, StatusStudentBell = new StatusStudentBell() { Bell1 = "I", Bell2 = "J", Bell3 = "K", Bell4 = "L"}}, 
       new StudentInfo() { FullName = "July", GroupName = "Pinapple", LessonStudy = "Gym", StudentId = 246, StatusStudentBell = new StatusStudentBell() { Bell1 = "M", Bell2 = "N", Bell3 = "O", Bell4 = "P"}} 
      }; 

      foreach (StudentInfo info in studentInfo) 
      { 
       dt.Rows.Add(new object[] { info.FullName, info.GroupName, info.LessonStudy, info.StudentId, info.StatusStudentBell.Bell1, info.StatusStudentBell.Bell2, info.StatusStudentBell.Bell3, info.StatusStudentBell.Bell4 }); 
      } 

      dataGridView1.DataSource = dt; 
     } 
     class StudentInfo 
     { 
      public int StudentId { get; set; } 
      public String FullName { get; set; } 
      public String LessonStudy { get; set; } 
      public String GroupName { get; set; } 
      public StatusStudentBell StatusStudentBell { get; set; } 
     } 
     class StatusStudentBell 
     { 
      public String Bell1 { get; set; } 
      public String Bell2 { get; set; } 
      public String Bell3 { get; set; } 
      public String Bell4 { get; set; } 
     } 
    } 
} 
+0

브라보, 아주 고마워. –