DataGridView动态更改数据源

本文关键字:数据源 动态 DataGridView | 更新日期: 2023-09-27 18:29:47

基本上,当我创建这个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());

DataGridView动态更改数据源

我也遇到了同样的问题,经过一段时间的搜索和测试,终于找到了解决方案:

        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需要是true,仅此而已。

如果进行

DataSource = null

考虑到IQueryable回报CurrencyManager:,最好更新货币管理器

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

CurrencyManager

CurrencyManager.Refresh()

更改行:

dgvMyPatients.DataSource = patients;

dgvMyPatients.DataSource = typeof(List<>);
dgvMyPatients.DataSource = patients.ToList();

例如,您可以尝试设置:

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

此外,您不需要每次更新源时都提供数据网格模式。

更新

工作样品:

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