コード例 #1
0
        private void GetCurrentCourseEnrollments()
        {
            // load the data from the store (the database)
            var courseEnrollments = studentStore.GetCourseEnrollmentsByStudentId(student.Id);

            // add columns to the data table--for non-editable columns,
            // get a ref to the new column and set it to read-only
            var idColumn = courseEnrollmentsDataTable.Columns.Add("Id");

            idColumn.ReadOnly = true;
            var courseColumn = courseEnrollmentsDataTable.Columns.Add("Course");

            courseColumn.ReadOnly = true;
            courseEnrollmentsDataTable.Columns.Add("Grade");

            // iterate over the data you got and add it, making sure to match the number of columns
            foreach (var courseEnrollment in courseEnrollments)
            {
                courseEnrollmentsDataTable.Rows.Add(
                    courseEnrollment.Id,
                    courseEnrollment.Course.Name,
                    courseEnrollment.Grade);
            }

            // set the data grid view's data source to your table to display the data
            CourseEnrollments.DataSource = courseEnrollmentsDataTable;
            courseEnrollmentsDataTable.AcceptChanges();     // this sets all the rows to "unmodified" state so you can detect the changed ones later
        }