无论何时在第一种形式的datagridview1中选择行,如何以新的形式获取datagridview2中的相关数据 [英] How to get related data in datagridview2 in a new form, whenever a row is selected in datagridview1 in the first form

查看:79
本文介绍了无论何时在第一种形式的datagridview1中选择行,如何以新的形式获取datagridview2中的相关数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我有两个窗口形式,在第一种形式我有我的datagridview1显示来自数据库的数据,我想在新的form2中弹出另一个datagridview2,它将显示根据datagridview1行的相关数据我点击。有可能这样做吗?我已经完成了创建datagridview1,我想在datagridview form2中显示它的连接数据。例如,我在form1中显示[id,name,location],当我选择行时,form2弹出并显示[types,date_installed],id = same。



ps:我已经创建了一个名为myClass的类,其中包含数据库的连接字符串。



以下是我在form1中的代码:





So basically i have two window forms which in the first form i have my datagridview1 that show data from a database, i would like to pop out another datagridview2 in a new form2, that will show related data according to the datagridview1 row i clicked. Is it possible to do so? I have done creating datagridview1 and i would like to show its connected data in datagridview form2. For example i have [id, name, location] displayed in form1, and when i select the row, form2 pop out and display [types, date_installed], with both id = same.

ps: i have already created a class named myClass that contains the connection string to my database.

Here are my codes in form1:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace MyTask
{
    public partial class Form1 : Form
    {
        private myClass myCls;
       
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myCls = new myClass();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = myCls.QueryData(@"select * from my_database ordered by database_id);

            dataGridView1.Refresh();

        }
 private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
             Form2 form2 = new Form2();
            form2.Show();
            form2.Refresh();
            dataGridView2.DataSource = dataGridView1.SelectedRows
            dataGridView2.Refresh();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}





我尝试了什么:



嗯我在我的dataGridView1中尝试过这个...





What I have tried:

Well i tried this in my dataGridView1...

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
             Form2 form2 = new Form2();
            form2.Show();
            form2.Refresh();
            dataGridView2.DataSource = dataGridView1.SelectedRows
            dataGridView2.Refresh();
        }

推荐答案

问题是Form2上的任何内容都是属性而你的父窗体不应该是能够访问它。如果Form1可以访问Form2上的控件,那么您可以在以后对Form2进行任何更改,而不必仔细考虑可能对Form1,Form3或任何可能使用Form2的代码产生的影响!



相反,使用Form2中的属性和/或构造函数来传递信息,并让它处理显示它想要的内容,以及它想要的内容。

见这里:< a href =https://www.codeproject.com/Tips/548052/Transferring-information-between-two-forms-Part>在两个表格之间传递信息,第1部分:父母与孩子 [ ^ ]

传递Form2所需的只是ID值:它从数据库中收集所需的内容,并在不需要知道Form1如何工作的情况下显示它:

The problem is that anything on Form2 is it's "Property" and your "parent form" should not be able to access it. If Form1 can access controls on Form2, then you can;t make any changes to Form2 in future without considering very carefully what affects that may have on existing code in Form1, Form3, or whatever might be using Form2!

Instead, use properties and / or constructors in Form2 to pass the information and let it deal with displaying what it wants, how it wants.
See here: Transferring information between two forms, Part 1: Parent to Child[^]
All you need to pass the Form2 is the ID value: it collects what infor it needs to from the DB, and displays it without Form1 needing to know how it works:
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    if (dgv != null && e.RowIndex >= 0)
        {
        int id = (int)dgv.Rows[e.RowIndex].Cells["ID"].Value;
        Form2 form2 = new Form2(id);
        form2.Show();
        }
    }

private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
    DataGridView dgv = sender as DataGridView;
    if (dgv != null && e.RowIndex >= 0)
        {
        int id = (int)dgv.Rows[e.RowIndex].Cells["ID"].Value;
        Form2 form2 = new Form2();
        form2.ID = id;
        form2.Show();
        }
    }


这篇关于无论何时在第一种形式的datagridview1中选择行,如何以新的形式获取datagridview2中的相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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