【问题标题】:Update DataGridView after marking the row of another - C#在标记另一个的行后更新 DataGridView - C#
【发布时间】:2018-04-13 11:46:44
【问题描述】:

所以我对编码和所有这些东西都很陌生,所以如果这是一个非常愚蠢的问题,请原谅,我已经尝试寻找它但没有找到任何对我有帮助的东西。

我的 WinForm 中有两个不同的 DataGrid,一个存储了有关客户的信息,另一个存储了不同的联系人信息 - 两者都从数据库中提取。 现在我需要以某种方式更新第二个网格,并且只显示与我选择的客户相关的联系人。

如何处理这个问题?

非常感谢您的帮助!

【问题讨论】:

  • 大概“主”网格的数据将包含一个 ID,它是“详细信息”数据的外键。弄清楚如何获取ID并查询其他数据WHERE ID = <that id>
  • 嗨,你能告诉我们你已经做了什么吗?以便我们了解如何为您提供帮助

标签: c# database winforms datagridview


【解决方案1】:

好的,我解决了它,并在这里分享给未来可能遇到同样问题的人。这就是函数的样子,可能不是世界上最漂亮的东西,但它可以运行。

        public void RefreshContact(int customerID)
    {
        CustomerDBEntities db = new CustomerDBEntities();
        var contacts = db.Contacts;         

        dataGridView_Contacts.Rows.Clear();
        if (customerID != 0)
        {
            int i = 0;
            foreach (var item in db.Customers.Where(x => x.PKCustomer == customerID).First().Contact.ToList())
            {
                dataGridView_Contacts.Rows.Add();
                dataGridView_Contacts.Rows[i].Cells[0].Value = item.PKContact;
                dataGridView_Contacts.Rows[i].Cells[1].Value = item.Name;
                dataGridView_Contacts.Rows[i].Cells[2].Value = item.Firstname;
                i++;
            }
        }
    }

这就是我调用函数的方式

        private void dataGridView_Customers_SelectionChanged(object sender, EventArgs e)
    {
        CustomerDBEntities db = new CustomerDBEntities();

        int customerID = (int)dataGridView_Customers.SelectedRows[0].Cells[0].Value;

        RefreshContacts(customerID);         
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-09
    • 2012-06-08
    • 1970-01-01
    • 2013-07-02
    相关资源
    最近更新 更多