本文介绍了DataGridView的更改数据源动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上当我创建这个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();
这篇关于DataGridView的更改数据源动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!