Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data;
  11. using System.Windows.Forms;
  12. using System.Data.SqlClient;
  13.  
  14. namespace DGV
  15. {
  16. public partial class Form1 : Form
  17. {
  18. private DataGridView _masterDGV = new DataGridView();
  19. private DataGridView _detailsDGV = new DataGridView();
  20. private BindingSource _masterBS = new BindingSource();
  21. private BindingSource _detailsBS = new BindingSource();
  22.  
  23. string _conStr = "Data Source=PC_KATEDRA\\MSSQL;Initial Catalog=Poliklinika;Integrated Security=SSPI";
  24.  
  25. public Form1()
  26. {
  27. InitializeComponent();
  28.  
  29. _masterDGV.Dock = DockStyle.Fill;
  30. _detailsDGV.Dock = DockStyle.Fill;
  31.  
  32. SplitContainer splitContainer1 = new SplitContainer();
  33. splitContainer1.Dock = DockStyle.Fill;
  34. splitContainer1.Orientation = Orientation.Horizontal;
  35. splitContainer1.Panel1.Controls.Add(_masterDGV);
  36. //splitContainer1.Panel2.Controls.Add(_detailsDGV);
  37. this.Controls.Add(splitContainer1);
  38.  
  39. this.Load += new System.EventHandler(Form1_Load);
  40. this.Text = "DataGridView master/detail demo";
  41. }
  42. private void Form1_Load(object sender, System.EventArgs e)
  43. {
  44. // Bind the DataGridViews to the corresponding BindingSource
  45. // ... 2 rows
  46. _masterDGV.DataSource = _masterBS;
  47. _detailsDGV.DataSource = _detailsBS;
  48.  
  49. // Load the ds from the database.
  50. GetData();
  51.  
  52. // Resize the columns:
  53. _masterDGV.AutoResizeColumns();
  54. _detailsDGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  55. }
  56. private void GetData()
  57. {
  58. try
  59. {
  60. //SqlConnection con = new SqlConnection(_conStr);
  61.  
  62. //DataSet ds = new DataSet();
  63. //ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
  64.  
  65.  
  66. //SqlDataAdapter masterDA = new SqlDataAdapter("select * from Pacienti", con);
  67. //masterDA.Fill(ds, "Pacienti");
  68. //// ... 2 rows
  69. //SqlDataAdapter detailsDA = new SqlDataAdapter("select * from Navstevy", con);
  70. //detailsDA.Fill(ds, "Navstevy");
  71.  
  72.  
  73. //DataRelation relation = new DataRelation("PacientiNavstevy",
  74. // ds.Tables["Pacienti"].Columns["idP"],
  75. // ds.Tables["Navstevy"].Columns["idP"]);
  76. //ds.Relations.Add(relation);
  77.  
  78. //// Bind the master ds connector to the Pacienti table.
  79. //_masterBS.DataSource = ds;
  80. //_masterBS.DataMember = "Pacienti";
  81.  
  82. //// Bind the details ds to the master ds using the relation PacientiNavstevy.
  83. //// ... 2 rows
  84. //_detailsBS.DataSource = _masterBS;
  85. //_detailsBS.DataMember = "PacientiNavstevy";
  86.  
  87.  
  88. }
  89. catch (SqlException)
  90. {
  91. MessageBox.Show(_conStr);
  92. }
  93. }
  94.  
  95. private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  96. {
  97.  
  98. comboBox2.Items.Clear();
  99.  
  100. string selected = comboBox1.GetItemText(this.comboBox1.SelectedItem);
  101. _conStr = "Data Source=PC_KATEDRA\\MSSQL;Initial Catalog=" +
  102. selected +
  103. ";Integrated Security=SSPI";
  104.  
  105.  
  106. using (SqlConnection sqlConnection = new SqlConnection(_conStr))
  107. {
  108. SqlCommand sqlCmd = new SqlCommand("SELECT TABLE_NAME FROM " + selected + ".INFORMATION_SCHEMA.Tables;", sqlConnection);
  109. sqlConnection.Open();
  110. SqlDataReader sqlReader = sqlCmd.ExecuteReader();
  111.  
  112. while (sqlReader.Read())
  113. {
  114. comboBox2.Items.Add(sqlReader["TABLE_NAME"].ToString());
  115. }
  116.  
  117. sqlReader.Close();
  118. }
  119. }
  120.  
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement