Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am creating a data table and trying to bind it wit a grid view.

Code

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

Now The Data table is Created Fine and all cause I used a breakpoint to see whether it was being created the thing is it is not populating the grid view. any ideas?
Posted
Updated 3-Jul-13 20:36pm
v2
Comments
Nirav Prabtani 4-Jul-13 2:38am    
Have you got count of timeTable???
thanh_bkhn 4-Jul-13 2:39am    
Your code seems fine. Is your gridTimeTable is properly declared?
komaliks 4-Jul-13 5:35am    
Yeah I am able to get data in the datatable timeTable.
Vipin kumar.P 4-Jul-13 7:29am    
Is your gridview is properly designed with proper bound fields
komaliks 4-Jul-13 8:46am    
yeah. its proper

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900