Philemon
Philemon

Reputation: 101

Unable to show data on data grid view

I have problems showing my data inside the data into the data grid view. Can anyone help to solve because they never prompt me any errors while compiling and there are also data inside the database. What comes out inside the data grid view is only the columns and no data inside.

private void LoadAllEmpShift()
    {
        using (testEntities Setupctx = new testEntities())
        {
            var Viewemp = from ES in Setupctx.employeeshifts
                          join shifthour sh in Setupctx.shifthours on ES.ShiftHourID equals sh.idShiftHours
                         select new
                         {
                             ES.idEmployeeShift,
                             ShiftHour_Start = sh.shiftTiming_start,
                             ShiftHour_Stop = sh.shiftTiming_stop,
                             ES.EmployeeName,
                             ES.StartTime,
                             ES.EndTime,
                             ES.Date
                         };
            dgvShift.DataSource = Viewemp;
        }
    }

Any help will be greatly appreciated.

Upvotes: 0

Views: 1234

Answers (1)

Chris Gessler
Chris Gessler

Reputation: 23113

After setting the DataSource property, you need to call

dgvShift.DataBind();

Edit:

I believe the above is for a DataGrid / GridView (in case anyone is using those controls).

For DataGridView, you need to have a BindingSource.

Add the BindingSource control to your form, then set the DataSource property of the BindingSource to Viewemp

dgvBindingSource.DataSource = Viewemp;
dgvShift.DataSource = dgvBindingSource; 

Upvotes: 1

Related Questions