本文介绍了无论何时在第一种形式的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(); } 推荐答案 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中的相关数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 00:45