user962206
user962206

Reputation: 16117

DataGridView Changing DataSource Dynamically

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

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

Upvotes: 4

Views: 33985

Answers (4)

Tyl
Tyl

Reputation: 5252

I had the same problem, after searched and tested a while, finally found the solution:

        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 needs be true, that's it.

Upvotes: 3

Azhar Khorasany
Azhar Khorasany

Reputation: 2709

Change the line:

dgvMyPatients.DataSource = patients;

To

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

Upvotes: 0

Dmitry Reznik
Dmitry Reznik

Reputation: 6862

For one, you could try setting:

DataSource = null;

before updating it. I'd personally recommend using BindingList for binding your data to a dataGridView. This way, you don't have to change dataSource - only the data contained in it. It's used like this:

BindingList<PatientInfo> data = new BindingList<PatientInfo>();
dgvMyPatients.DataSource = data;

...
public void fillDataGrid(IQueryable<PatientInfo> patients)
{
    data.Clear();
    data.AddRange(patients);
}

Also, you wouldn't need to feed datagrid schema each time you update a source.

UPDATE

Working sample:

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

Upvotes: -1

A G
A G

Reputation: 22559

Instead if doing

DataSource = null

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

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

CurrencyManager

CurrencyManager.Refresh()

Upvotes: 2

Related Questions