数据表未绑定到网格视图

时间:2012-04-27 21:23:09

标签: c# datagrid datagridview datatable

我正在创建一个数据表并试图用网格视图绑定它。

代码:

        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();
            }
        }

现在数据表已经精确创建了所有原因我使用断点来查看它是否正在被创建,因为它没有填充网格视图。任何想法?

谢谢:)

0 个答案:

没有答案