DataGridView的更改数据源动态数据源、动态、DataGridView

2023-09-06 04:55:36 作者:亡命 -destructi

基本上当我创建这个DataGridView中我有这个$ C $三来一补起来。

Basically when I create this DataGridView I have this code to fill it up

public void fillDataGrid(IQueryable<PatientInfo> patients) {

            dgvMyPatients.DataSource = patients;

            dgvMyPatients.Columns["Pat_Last_Name"].DisplayIndex = 0;
            dgvMyPatients.Columns["Pat_First_Name"].DisplayIndex = 1;
            dgvMyPatients.Columns["Pat_Middle_Name"].DisplayIndex = 2;
            dgvMyPatients.Columns["Pat_First_Name"].HeaderText = "First Name";
            dgvMyPatients.Columns["Pat_Last_Name"].HeaderText = "Last Name";
            dgvMyPatients.Columns["Pat_Middle_Name"].HeaderText = "Middle Name";

        }

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

所以,当我创建我的对象我只是这样做

So when I create my Object I just do this

fillDataGrid(showMyPatients());

不过,当我点击一个按钮,我想改变这个查询的内容类似

But When I click a button I want to change its contents to something like in 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;
    }

后来,当我点击我的按钮,它会做到这一点,但它没有更新的数据网格这是为什么?     fillDataGrid(searchPatient());

Then when I click my button It will do this, but it is not updating the datagrid why is that? fillDataGrid(searchPatient());

推荐答案

相反,如果这样做

DataSource = null

其更好地刷新货币管理,给予IQueryable的返回的CurrencyManager:

its better to refresh the currency manager, given IQueryable returns CurrencyManager:

 (dgvMyPatients.BindingContext[dataGridView1.DataSource] as CurrencyManager).Refresh();

的CurrencyManager

CurrencyManager.Refresh()