fMain.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace СУБД_Кадры
  12. {
  13. public partial class fMain : Form
  14. {
  15. public fMain()
  16. {
  17. InitializeComponent();
  18. }
  19. public static string TxtCon = "Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\SUBD_Kadr.mdf;Integrated Security = True; Connect Timeout = 30\r\n";
  20. struct Client
  21. {
  22. public string ID, Surname,Name,Pathronymic, Email, Phone, Gender;
  23. }
  24. List<Client> LstClient = new List<Client>();
  25. int CountPerPage = 5,NBegin=0;
  26. void GetDataFromDB()
  27. {
  28. NBegin = 0;
  29. SqlConnection con = new SqlConnection(TxtCon);
  30. con.Open();
  31. string Uslovie = "";
  32. if (CmbFind.Text == "фамилии")
  33. Uslovie += $" where Surname Like '{TbxFind.Text}%'";
  34. if (CmbFind.Text == "имени")
  35. Uslovie += $" where Name Like '{TbxFind.Text}%'";
  36. if (CmbFind.Text == "отчеству")
  37. Uslovie += $" where Pathronymic Like '{TbxFind.Text}%'";
  38. string Gender = "";
  39. if (CmbGender.Text == "мужской")
  40. Gender += " and Gender = 'мужской'";
  41. if (CmbGender.Text == "женский")
  42. Gender += " and Gender = 'женский'";
  43. string Sort = "";
  44. if (CmbSort.Text == "фамилии")
  45. Sort += " order by Surname";
  46. SqlCommand comm = new SqlCommand($"Select ID_Client,Surname,Name,Pathronymic,Phone,Email,Gender From Client" +Uslovie+Gender+Sort,con);
  47. SqlDataReader Res = comm.ExecuteReader();
  48. LstClient.Clear();
  49. while(Res.Read())
  50. {
  51. Client NEWclient = new Client();
  52. NEWclient.ID = Res["ID_Client"].ToString();
  53. NEWclient.Surname = Res["Surname"].ToString();
  54. NEWclient.Name = Res["Name"].ToString();
  55. NEWclient.Pathronymic = Res["Pathronymic"].ToString();
  56. NEWclient.Email = Res["Email"].ToString();
  57. NEWclient.Phone = Res["Phone"].ToString();
  58. NEWclient.Gender = Res["Gender"].ToString();
  59. LstClient.Add(NEWclient);
  60. }
  61. con.Close();
  62. }
  63. void FillDgvClient()
  64. {
  65. DgvClients.Rows.Clear();
  66. for(int i=NBegin;i<NBegin+CountPerPage;i++)
  67. {
  68. if (i > LstClient.Count - 1)
  69. break;
  70. string Mess = $"{LstClient[i].Surname} {LstClient[i].Name} {LstClient[i].Pathronymic}\r\nТелефон: {LstClient[i].Phone} Почта: {LstClient[i].Email} \r\n Пол: {LstClient[i].Gender}";
  71. DgvClients.Rows.Add(Mess,LstClient[i].ID);
  72. }
  73. int Cnt = LstClient.Count / CountPerPage;
  74. if (LstClient.Count % CountPerPage != 0)
  75. Cnt++;
  76. PanelPages.Controls.Clear();
  77. LblBack.Dock = DockStyle.Left;
  78. LblNext.Dock = DockStyle.Left;
  79. PanelPages.Controls.Add(LblNext);
  80. for (int i = Cnt; i > 0; i--)
  81. {
  82. Label Lbl = new Label();
  83. Lbl.Text = i.ToString();
  84. Lbl.Dock = DockStyle.Left;
  85. Lbl.AutoSize = true;
  86. Lbl.Click += Lbl_Click;
  87. //если отоброжается номер текущей страницы
  88. if ((NBegin + CountPerPage) / CountPerPage == i)
  89. {
  90. Lbl.BackColor = Color.LightGray;
  91. Lbl.Font = new Font(Lbl.Font, FontStyle.Underline);
  92. }
  93. PanelPages.Controls.Add(Lbl);
  94. }
  95. PanelPages.Controls.Add(LblBack);
  96. TsIInfo.Text = $"Всего записей: {LstClient.Count} отображается: {DgvClients.RowCount}";
  97. }
  98. private void fMain_Load(object sender, EventArgs e)
  99. {
  100. CmbSort.SelectedIndex = 0;
  101. CmbFind.SelectedIndex = 0;
  102. CmbGender.SelectedIndex = 0;
  103. GetDataFromDB();
  104. FillDgvClient();
  105. }
  106. private void LblBack_Click(object sender, EventArgs e)
  107. {
  108. if (NBegin > 0)
  109. {
  110. NBegin = NBegin - CountPerPage;
  111. FillDgvClient();
  112. }
  113. }
  114. private void LblNext_Click(object sender, EventArgs e)
  115. {
  116. if (NBegin + CountPerPage < LstClient.Count)
  117. {
  118. NBegin = NBegin + CountPerPage;
  119. FillDgvClient();
  120. }
  121. }
  122. private void TbxFind_TextChanged(object sender, EventArgs e)
  123. {
  124. GetDataFromDB();
  125. FillDgvClient();
  126. }
  127. private void CmbGender_SelectedIndexChanged(object sender, EventArgs e)
  128. {
  129. GetDataFromDB();
  130. FillDgvClient();
  131. }
  132. private void CmbSort_SelectedIndexChanged(object sender, EventArgs e)
  133. {
  134. GetDataFromDB();
  135. FillDgvClient();
  136. }
  137. private void bAdd_Click(object sender, EventArgs e)
  138. {
  139. fAddClient FAdd = new fAddClient();
  140. FAdd.ShowDialog();
  141. int CurrentNBegin = NBegin;
  142. GetDataFromDB();
  143. NBegin = CurrentNBegin;
  144. FillDgvClient();
  145. }
  146. private void BtnDel_Click(object sender, EventArgs e)
  147. {
  148. DialogResult Res = MessageBox.Show("Вы действительно хотите удалить выделенного клиента?", "Внимание!!!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  149. if (Res == DialogResult.No)
  150. return;
  151. SqlConnection con = new SqlConnection(TxtCon);
  152. con.Open();
  153. string TxtQuery1 = $"delete from Client where ID_Client={DgvClients.CurrentRow.Cells[1].Value}";
  154. SqlCommand comm1 = new SqlCommand(TxtQuery1, con);
  155. comm1.ExecuteNonQuery();
  156. con.Close();
  157. int CurrentNBegin = NBegin;
  158. GetDataFromDB();
  159. NBegin = CurrentNBegin;
  160. FillDgvClient();
  161. }
  162. private void Lbl_Click(object sender, EventArgs e)
  163. {
  164. int n = int.Parse((sender as Label).Text);
  165. NBegin = (n - 1) * CountPerPage;
  166. FillDgvClient();
  167. }
  168. }
  169. }