基本上,当我创建这个DataGridView时,我有这个代码来填充它
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;
}
因此,当我创建我的对象时,我就这样做了
fillDataGrid(showMyPatients());
但是当我点击一个按钮时,我想将其内容更改为此查询中的内容
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());
答案 0 :(得分:3)
我有同样的问题,经过搜索和测试一段时间后,终于找到了解决方案:
a.First()
DataTable dt = new DataTable();
dt.Columns.Add("Column One");
dt.Rows.Add(new object[] { "Item1" });
dt.Rows.Add(new object[] { "Item2" });
dt.Rows.Add(new object[] { "Item3.3" });
this.dataGridView1.AutoGenerateColumns = true;
this.dataGridView1.Columns.Clear();
//dataGridView1.DataSource = null;
dataGridView1.DataSource = dt;
需要AutoGenerateColumns
,这就是它。
答案 1 :(得分:2)
相反,如果做
DataSource = null
最好刷新货币经理,因为IQueryable返回CurrencyManager:
(dgvMyPatients.BindingContext[dataGridView1.DataSource] as CurrencyManager).Refresh();
答案 2 :(得分:0)
更改行:
dgvMyPatients.DataSource = patients;
到
dgvMyPatients.DataSource = typeof(List<>);
dgvMyPatients.DataSource = patients.ToList();
答案 3 :(得分:-1)
首先,您可以尝试设置:
DataSource = null;
在更新之前。我个人建议使用BindingList将数据绑定到dataGridView。这样,您不必更改dataSource - 仅包含其中包含的数据。它的使用方式如下:
BindingList<PatientInfo> data = new BindingList<PatientInfo>();
dgvMyPatients.DataSource = data;
...
public void fillDataGrid(IQueryable<PatientInfo> patients)
{
data.Clear();
data.AddRange(patients);
}
此外,每次更新源时都不需要提供datagrid架构。
<强>更新强>
工作样本:
public partial class Form1 : Form
{
private BindingList<SomeClass> _data = new BindingList<SomeClass>();
public Form1()
{
InitializeComponent();
dataGridView1.DataSource = _data;
_data.Add(new SomeClass() { First = "1", Second = "1", Third = "1" });
_data.Add(new SomeClass() { First = "2", Second = "2", Third = "2" });
_data.Add(new SomeClass() { First = "3", Second = "3", Third = "3" });
_data.Add(new SomeClass() { First = "4", Second = "4", Third = "4" });
_data.Add(new SomeClass() { First = "5", Second = "5", Third = "5" });
_data.Add(new SomeClass() { First = "6", Second = "6", Third = "6" });
_data.Add(new SomeClass() { First = "7", Second = "7", Third = "7" });
_data.Add(new SomeClass() { First = "8", Second = "8", Third = "8" });
}
private void button1_Click(object sender, EventArgs e)
{
_data.Clear();
_data.Add(new SomeClass() { First = "11", Second = "11", Third = "11" });
_data.Add(new SomeClass() { First = "21", Second = "21", Third = "21" });
_data.Add(new SomeClass() { First = "31", Second = "31", Third = "31" });
}
}
public class SomeClass
{
public string First { get; set; }
public string Second { get; set; }
public string Third { get; set; }
}