Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

c# - Filling two different datagridview with different tables from same DB

I'm working on a vet app, using C# and SQL. When I have access to the client history, it fills my pacient datagridview. I would like to know how to fill the clinic history datagridview from the pacient datagridview while I have selected the rows of the pacient. Already made the ClearSelection() from load to deselect any pacient, but I tried to make the SelectedRow event, and nothing happens on the clinic history datagridview. If needed, I can put the code or pictures later.

PS: clinic history table has foreign key linked to pacient table.

EDIT: Here is the code I wrote. GetData gets the pacient's table and GetData2 the clinichistory's table.

    private void GetData(string selectCommand)
    {
        try
        {
            dataAdapter = new SqlDataAdapter(selectCommand, connString);
            table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            bindingSource17.DataSource = table;
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    private void GetData2(string selectCommand)
    {
        dataGridView3.DataSource = null;
        try
        {
            dataAdapter = new SqlDataAdapter(selectCommand, connString);
            table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            dataAdapter.Fill(table);
            bindingSource18.DataSource = table;
        }
        catch (SqlException ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

//Here is the part that confuses me, since the pacient table works perfectly, but not so the clinichistory table. The following works with a client search, which returns the pacient owned by the client

   private void button12_Click_1(object sender, EventArgs e)
    {
        Form7 Buscarcli = new Form7();
        Buscarcli.TransfEvent += frm_TransfEvent;
        Buscarcli.ShowDialog();
        dataGridView2.DataSource = bindingSource17;
        if (lblID.Text != null)
        {
            GetData("Select * from Pacientes where id_pacientes like '%" + lblID.Text + "%'");
        }
    }

//After this, Idk how to continue to make it work. Bindingsource17 is the datagridview for pacients, and Bindingsource18 the same but for clinichistory.

Thank you very much. PS: I have a few weeks coding experiencie, so sorry if it looks like a complete mess. I do what I can.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Nothing like what you've been doing, I'm pretty sure. The easy way is:

  • Add a new dataset to your project
  • Open it, right click the surface, choose "Add TableAdapter", configure the connectionstring
  • Add a query of something like SELECT * FROM Patient WHERE ID = @id
  • Finish the Wizard, calling your query FillById/GetDataById
  • Add another query; rigt click the tableadapter, add query.. SELECT * FROM Patient WHERE lastName LIKE @lastName - or whatever you will search patients by
  • Call it FillByLastName (or whatever)
  • Add another tableadapter - SELECT * FROM ClinicHistory WHERE ID = @Id, FillById etc, finish
  • Add another wuery to this one; SELECT * FROM ClinicHistory WHERE PatientID = @PatientID, FillByPatientId etc, finish etc
  • Save the data set
  • Switch to the form
  • SHow the datasources window (View menu, other windows)
  • Drag the Patients node onto the form
  • Expand the Patients node
  • Drag the ClinicHistory, that is a child of the Patients table node NOT the one that is a peer of it, onto the form
  • Switch to code, find the line that fills the Patients table from the toolstriptextbox, add these lines under it:
    clinicHistoryTableAdapter.ClearBeforeFill = false;
    foreach(PatientsRow ro in yourDataSetName.Patients)
      clinicHistoryTableAdapter.FillByPatientId(yourDataSetName.ClinicHistory, ro.Id);

And change the line itself so it reads more like:

patientsTableAdapter.FillByLastName(yourDataSetName.Patients, idToolStripTextBox.Text); //todo: rename that textbox

Adjust names for your context (I guessed)

Run the app; put a patient name into the top navigator (text box) and click Fill.. Many patients (hopefuily) will load.. And when you click on any one of them the ClinicHistory table auto updates to show the related data


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...