KyelJmD
KyelJmD

Reputation: 4732

DataGridView is blank after Changing DataSource at Runtime

I have a form where when it was created it will this query

 public IQueryable<PatientInfo> showMyPatients() 
 {    
        DbClassesDataContext myDb = new DbClassesDataContext(dbPath);
        var patientInfo = from patients in myDb.PatientInfos
                          where patients.Phy_ID == physcianID
                          select patients;

        return patientInfo;
  }

Once I call that I will insert it to a variable and pass it as a paramaters here

public void fillDataGrid(IQueryable<PatientInfo> patients) 
{                
       dgvMyPatients.DataSource = patients;
}

Sounds simple right? but when I run this query

private IQueryable<PatientInfo> searchPatient() 
{    
      DbClassesDataContext myDb = new DbClassesDataContext(dbPath);
      var search = from myPatients in myDb.PatientInfos
                   where (myPatients.Pat_ID == patient_ID && myPatients.Pat_First_Name.Contains(txtSearch.Text)) ||
                         (myPatients.Pat_ID == patient_ID && myPatients.Pat_Last_Name.Contains(txtSearch.Text)) ||
                        (myPatients.Pat_ID == patient_ID && myPatients.Pat_Middle_Name.Contains(txtSearch.Text))
                         select myPatients;

       return search;
 }

and pass it as a parameter to fillDataGrid() the DataGrid will have a blank value even though there is a result from that query

BindingSource bs = new BindingSource();
bs.DataSource = searchPatient();
dgvMyPatients.DataSource = bs;

NOTE: Both of the queries return PATIENTINFO and I would be inserting in the same DataGrid

Upvotes: 2

Views: 5700

Answers (2)

KyelJmD
KyelJmD

Reputation: 4732

What I did was, Replace my connection string , then changed in my query, there seems to be problem on my Query soo that is why it is not working. it is not producing any result.

So what I did was, each time I call this event and change the datasource I use dgvMyPatients.Columns.Clear(); and dgvMyPatients.Refresh();

Upvotes: 2

Steve
Steve

Reputation: 216343

Try to add these lines

dgvMyPatients.Columns.Clear();
dgvMyPatients.AutoGenerateColumns = true;
dgvMyPatients.DataSource = bs;

Upvotes: 2

Related Questions