Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Data;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace DGV
- {
- public partial class Form1 : Form
- {
- private DataGridView _masterDGV = new DataGridView();
- private DataGridView _detailsDGV = new DataGridView();
- private BindingSource _masterBS = new BindingSource();
- private BindingSource _detailsBS = new BindingSource();
- string _conStr = "Data Source=PC_KATEDRA\\MSSQL;Initial Catalog=Poliklinika;Integrated Security=SSPI";
- public Form1()
- {
- InitializeComponent();
- _masterDGV.Dock = DockStyle.Fill;
- _detailsDGV.Dock = DockStyle.Fill;
- SplitContainer splitContainer1 = new SplitContainer();
- splitContainer1.Dock = DockStyle.Fill;
- splitContainer1.Orientation = Orientation.Horizontal;
- splitContainer1.Panel1.Controls.Add(_masterDGV);
- //splitContainer1.Panel2.Controls.Add(_detailsDGV);
- this.Controls.Add(splitContainer1);
- this.Load += new System.EventHandler(Form1_Load);
- this.Text = "DataGridView master/detail demo";
- }
- private void Form1_Load(object sender, System.EventArgs e)
- {
- // Bind the DataGridViews to the corresponding BindingSource
- // ... 2 rows
- _masterDGV.DataSource = _masterBS;
- _detailsDGV.DataSource = _detailsBS;
- // Load the ds from the database.
- GetData();
- // Resize the columns:
- _masterDGV.AutoResizeColumns();
- _detailsDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
- }
- private void GetData()
- {
- try
- {
- //SqlConnection con = new SqlConnection(_conStr);
- //DataSet ds = new DataSet();
- //ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
- //SqlDataAdapter masterDA = new SqlDataAdapter("select * from Pacienti", con);
- //masterDA.Fill(ds, "Pacienti");
- //// ... 2 rows
- //SqlDataAdapter detailsDA = new SqlDataAdapter("select * from Navstevy", con);
- //detailsDA.Fill(ds, "Navstevy");
- //DataRelation relation = new DataRelation("PacientiNavstevy",
- // ds.Tables["Pacienti"].Columns["idP"],
- // ds.Tables["Navstevy"].Columns["idP"]);
- //ds.Relations.Add(relation);
- //// Bind the master ds connector to the Pacienti table.
- //_masterBS.DataSource = ds;
- //_masterBS.DataMember = "Pacienti";
- //// Bind the details ds to the master ds using the relation PacientiNavstevy.
- //// ... 2 rows
- //_detailsBS.DataSource = _masterBS;
- //_detailsBS.DataMember = "PacientiNavstevy";
- }
- catch (SqlException)
- {
- MessageBox.Show(_conStr);
- }
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- comboBox2.Items.Clear();
- string selected = comboBox1.GetItemText(this.comboBox1.SelectedItem);
- _conStr = "Data Source=PC_KATEDRA\\MSSQL;Initial Catalog=" +
- selected +
- ";Integrated Security=SSPI";
- using (SqlConnection sqlConnection = new SqlConnection(_conStr))
- {
- SqlCommand sqlCmd = new SqlCommand("SELECT TABLE_NAME FROM " + selected + ".INFORMATION_SCHEMA.Tables;", sqlConnection);
- sqlConnection.Open();
- SqlDataReader sqlReader = sqlCmd.ExecuteReader();
- while (sqlReader.Read())
- {
- comboBox2.Items.Add(sqlReader["TABLE_NAME"].ToString());
- }
- sqlReader.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement