使用来自同一数据库的不同表填充两个不同的datagridview [英] Filling two different datagridview with different tables from same DB

查看:107
本文介绍了使用来自同一数据库的不同表填充两个不同的datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和SQL开发兽医应用程序。当我可以访问客户历史记录时,它将填充我的实际数据网格视图。我想知道如何在选择Pacient的行的同时从pacient datagridview填充临床历史datagridview。已经从加载中清除ClearSelection()以取消任何选择,但是我尝试进行SelectedRow事件,并且在临床历史数据gridview上没有任何反应。
如果需要,我可以稍后再放置代码或图片。

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:诊所历史记录表具有链接到pacient表的外键。

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

编辑:这是我编写的代码。

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

//这是让我感到困惑的部分,因为pacient表起作用了完美,但临床历史记录表却并非如此。以下与客户端搜索一起使用,它返回客户端拥有的权限

//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 + "%'");
        }
    }

//此后,Idk如何继续使其工作。 Bindingsource17是专人使用的datagridview,Bindingsource18是相同的,但适用于临床历史。

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

非常感谢。
PS:我有几个星期的编码经验,所以如果看起来像是一团糟,那么请抱歉。我会尽我所能。

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.

推荐答案

与您一直在做的事情不一样,我敢肯定。简单的方法是:

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


  • 将新数据集添加到项目中

  • 打开它,右键单击表面,选择添加TableAdapter,配置连接字符串

  • 添加类似 SELECT * FROM Patient WHERE ID = @id 的查询li>
  • 完成向导,调用查询FillById / GetDataById

  • 添加另一个查询;右键单击tableadapter,添加查询。.选择*从患者的姓氏为@lastName -或通过以下方式搜索患者

  • 称它为FillByLastName(或其他名称)

  • 添加另一个表适配器- SELECT * FROM ClinicHistory WHERE ID = @Id ,FillById等,完成

  • 为此添加另一个wuery; SELECT *从ClinicHistory中,PatientID = @PatientID ,FillByPatientId等,完成等

  • 保存数据集

  • 切换到表单

  • 如何显示数据源窗口(查看菜单,其他窗口)

  • 将患者节点拖到表单上

  • 展开患者节点

  • 拖动ClinicHistory,它是患者表节点的子节点,不是与其对等的节点,转到表单上

  • 切换到代码,从toolstriptextbox中找到填充Patients表的行,在其下面添加以下行:

  • 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)

运行应用;将患者姓名放入顶部导航器(文本框),然后单击填充。许多患者(希望)将加载。当您单击其中任何一个时,ClinicHistory表都会自动更新以显示相关数据

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

这篇关于使用来自同一数据库的不同表填充两个不同的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆