DataGridView Dynamically Change DataSource

Basically when I create this DataGridView I have this code to populate it

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 on the button, I want to change its content to something like this request

 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 the "It" button it will do it, but it doesn't update the datagrid, why is it? fillDataGrid (searchPatient ());

+3


source to share


4 answers


Have the same question, after searching, finally found the answer:

        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 must be true what it is.

+3


source


Instead, doing

DataSource = null

      

it's better to update your currency manager given that IQueryable returns CurrencyManager:



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

      

CurrencyManager

CurrencyManager.Refresh ()

+2


source


Change the line:

dgvMyPatients.DataSource = patients;

      

To

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

      

0


source


First, you can try installing:

DataSource = null;

      

before updating it. I personally recommend using a BindingList to bind data to a dataGridView. This way, you don't need to change the dataSource - only the data it contains. It is used as follows:

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

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

      

Also, you won't need to feed the datagrid every time the source is updated.

UPDATE

Working example:

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

      

-1


source







All Articles