Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
        {

        }
    }
}


What I have tried:

Well i tried this in my dataGridView1...

C#
private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
             Form2 form2 = new Form2();
            form2.Show();
            form2.Refresh();
            dataGridView2.DataSource = dataGridView1.SelectedRows
            dataGridView2.Refresh();
        }
Posted
Updated 9-May-18 22:10pm

1 solution

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();
        }
    }
Or
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();
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900