DataGridView is blank after Changing DataSource at Runtime

試著忘記壹切 提交于 2019-12-11 03:37:35

问题


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


回答1:


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




回答2:


Try to add these lines

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


来源:https://stackoverflow.com/questions/9963416/datagridview-is-blank-after-changing-datasource-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!