Fly to the sky & Return

DatagridView Row를 클릭하면 관련된 정보를 다른 datagridView에 표시하기 본문

프로그래밍/c# 윈폼 데이터베이스 기초부터

DatagridView Row를 클릭하면 관련된 정보를 다른 datagridView에 표시하기

낼은어떻게 2016. 3. 7. 16:00
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

폼의 구성은 다음과 같이  두개의 Datagridview를 폼위에 올려 놓는 것입니다.



이제  DataGridView1_CellContentClick 이벤트를 작성합니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
 
            string ID_1 = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
          
            string DB_path = targetPath;
            DataSet ds = new DataSet();
 
 
            DataConn conn1 = new DataConn();
 
 
            string sql = @"SELECT Customers.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Orders.ShipVia, Orders.Freight, Orders.ShipAddress, Orders.ShipName, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry
                          FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
                          WHERE (((Customers.CustomerID)='" + ID_1 + "'))";
 
 
            ds = conn1.GetDataset(sql, DB_path);
 
           
          
            dataGridView2.DataSource = ds.Tables[0];
 
           
        }
 
 
 
cs


결과는 다음과 같이 나타납니다.



어떻게 잘 되고 계신가여?


전체 코드는 다음과 같습니다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
 
namespace brog_1
{
    public partial class Form1 : Form
    {
        public string targetPath = Application.StartupPath + @"\Northwind.mdb";
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            DateTime aa = dateTimePicker1.Value;
            string date11 = aa.ToShortDateString();
 
            DataConn conn1 = new DataConn();
            DataSet ds;
            string DB_path = targetPath;
 
 
              string sql = @"SELECT * FROM Customers";
 
            ds = conn1.GetDataset(sql, DB_path);
            dataGridView1.DataSource = ds.Tables[0];
        }
 
        
 
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
           
 
            string ID_1 = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
          
            string DB_path = targetPath;
            DataSet ds = new DataSet();
 
 
            DataConn conn1 = new DataConn();
 
 
            string sql = @"SELECT Customers.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Orders.ShipVia, Orders.Freight, Orders.ShipAddress, Orders.ShipName, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry
                          FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
                          WHERE (((Customers.CustomerID)='" + ID_1 + "'))";
 
 
            ds = conn1.GetDataset(sql, DB_path);
 
           
          
            dataGridView2.DataSource = ds.Tables[0];
 
           
        }
    }
 
    class DataConn
    {
        public DataSet GetDataset(string sql, string DB_path)
        {
            string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_path + ";Jet OLEDB:Database Password=";
           
 
            OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connStr);
            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter(sql, conn);
            adp.Fill(ds);
            return ds;
 
        }
 
      
    }
}
 
 
 
cs