0

我正在创建一个数据表并尝试将其与网格视图绑定。

代码:

        DataTable timeTable = new DataTable();

        DataColumn colSubject = new DataColumn("Subject Name");
        DataColumn colClass = new DataColumn("Class");
        DataColumn colTeacher = new DataColumn("Lecturer");
        DataColumn colStartTime = new DataColumn("Start");
        DataColumn colEndTime = new DataColumn("End");

        colSubject.DataType = System.Type.GetType("System.String");
        colClass.DataType = System.Type.GetType("System.String");
        colTeacher.DataType = System.Type.GetType("System.String");
        colStartTime.DataType = System.Type.GetType("System.String");
        colEndTime.DataType = System.Type.GetType("System.String");

        timeTable.Columns.Add(colSubject);
        timeTable.Columns.Add(colClass);
        timeTable.Columns.Add(colTeacher);
        timeTable.Columns.Add(colStartTime);
        timeTable.Columns.Add(colEndTime);


        int numOfRows = 0;

        SqlDataReader read = null;
        try
        {

            conn.Open();
            read = getTimetable.ExecuteReader();

            if (read.HasRows)
            {
                while (read.Read())
                {
                    if (numOfRows < 3)
                    {
                        DataRow row = timeTable.NewRow();
                        row[colSubject] = read["subject_name"].ToString();
                        row[colClass] = read["resource_name_number"].ToString();
                        row[colTeacher] = read["userDetail_name"].ToString();
                        row[colStartTime] = read["timeSlot_startTime"].ToString();
                        row[colEndTime] = read["timeSlot_endTime"].ToString();

                        timeTable.Rows.Add(row);
                        numOfRows++;
                    }

                }

                gridTimeTable.DataSource = timeTable;
                gridTimeTable.DataBind();
            }
        }

现在数据表创建得很好,所有原因我使用了一个断点来查看它是否正在创建,事情是它没有填充网格视图。有任何想法吗?

谢谢 :)

4

0 回答 0