How to Navigate Records Based on DataGridView Using C#

This time, let’s learn how to navigate records based on datagridview using C#. This tutorial will show to you that you can put limitation on your displayed records in the datagridview. Sometimes, it’s stressful to scroll down if you are looking for a record that contains a lot of data. Well, this tutorial is a big help for you because you don’t have to scroll down anymore. You can now easily find the record that you’re looking for in just a click. Simply click the next and previous button to see other displayed records.

Creating Database

Create a database named "dbsubjects". Create a table in the database that you have created.
  1. CREATE TABLE `subject` (
  2.   `SUBJ_ID` int(11) NOT NULL,
  3.   `SUBJ_CODE` varchar(30) NOT NULL,
  4.   `SUBJ_DESCRIPTION` varchar(255) NOT NULL,
  5.   `UNIT` int(2) NOT NULL,
  6.   `PRE_REQUISITE` varchar(30) NOT NULL DEFAULT 'None'
Add the data in the table.
  1. INSERT INTO `subject` (`SUBJ_ID`, `SUBJ_CODE`, `SUBJ_DESCRIPTION`, `UNIT`, `PRE_REQUISITE`) VALUES
  2. (8, 'English Plus', 'English Plus', 3, 'Nones'),
  3. (14, 'NSTP1', 'National Service Training Program 1', 3, 'None'),
  4. (15, 'PE1', 'Physical Education 1', 2, 'None'),
  5. (16, 'HUMAN', 'Humanities', 3, 'None'),
  6. (17, 'COMART2', 'Communication Arts 2', 3, 'COMART1'),
  7. (18, 'COPRO-2', 'Computer Programming 2', 4, 'COPRO1'),
  8. (19, 'DATSTRUC', 'Data Structures', 4, 'COPRO1'),
  9. (20, 'DISTRUC', 'Discrete Structure', 3, 'ALGEBRA'),
  10. (21, 'NSTP2', 'National Service Training Program 2', 3, 'None'),
  11. (22, 'INTROART', 'Introduction to Arts', 3, 'None'),
  12. (23, 'PE2', 'Physical Education 2', 2, 'PE1'),
  13. (24, 'TRIGO', 'Trigonometry', 3, 'ALGEBRA'),
  14. (25, 'COMART3', 'Communication Arts 3', 3, 'COMART2'),
  15. (26, 'COPRO-3', 'Computer Programming 3', 4, 'COPRO2'),
  16. (27, 'LOGIC', 'Logic Design and Switching', 3, 'DISTRUC'),
  17. (28, 'PHILGOV', 'Philippine Government', 3, 'None'),
  18. (29, 'PHYSICS1', 'Physics 1', 4, 'None'),
  19. (30, 'STAT', 'Statistics', 3, 'ALGEBRA'),
  20. (31, 'SOCSTUD', 'Social Studies', 3, 'None'),
  21. (32, 'PE3', 'Physical Education 3', 2, 'PE1'),
  22. (33, 'COMART4', 'Communication Arts 4', 3, 'COMART3'),
  23. (34, 'ASSEM', 'Assembly Language', 4, 'LOGIC'),
  24. (35, 'PHILO', 'Philosophy', 3, 'None'),
  25. (36, 'SADSIGN', 'System Analysis and Design', 3, 'COPRO1'),
  26. (37, 'PHYSICS2', 'Physics 2', 4, 'Physics 1'),
  27. (38, 'DBSYS', 'Theory Database Systems', 3, 'DATSTRUC'),
  28. (39, 'PE4', 'Physical Education 4', 2, 'PE1'),
  29. (40, 'Eng 111', 'CommunicationArts 1', 3, 'none'),
  30. (41, 'Fil 111', 'Kom sa Akad. Fil', 3, 'NONE'),
  31. (42, 'Math 1', 'Basic math ', 3, 'NONE'),
  32. (43, 'SCE 111', 'Earth Science', 3, 'NONE '),
  33. (44, 'Lit 111', 'Philippines Literature ', 3, 'NONE'),
  34. (45, 'Rdg 1', 'Dev. Reading 1', 3, 'NONE'),
  35. (46, 'Psych 116', 'General Psychology', 3, 'NONE'),
  36. (47, 'PE 111', ' Physical Fitness', 2, 'NONE'),
  37. (48, 'NSTP 111', 'National Service training program 1 ', 3, 'NONE'),
  38. (49, 'Eng 211', 'speech & oral communication', 3, 'Eng 121'),
  39. (50, 'Fil 211', 'Masining na pagpapahayag', 3, 'Fil 121'),
  40. (51, 'Lit 121 ', 'World literature', 3, 'Lit 111'),
  41. (52, 'SocSci 132', 'Rizal & other heroes ', 3, 'NONE'),
  42. (53, 'Ed 211', 'Child & adolescent Psychology', 3, 'Psyh 116'),
  43. (54, 'Ed 212', 'Education Technology 1', 3, 'NONE'),
  44. (55, 'FS 1', 'Observation of learners Dev\'t & the school enviroment  ', 3, 'NONE'),
  45. (56, 'Eng 212', 'The Teaching Of speaking ', 3, 'NONE '),
  46. (57, 'Eng 213', 'The teaching of lsting & Rdg.', 2, 'NONE'),
  47. (58, 'PE 211', 'Team sports', 2, 'PE 121 '),
  48. (59, 'Stat 115', 'Intro. to Statistics ', 3, 'NONE'),
  49. (60, 'Ed 311', 'The teaching profession ', 3, 'NONE'),
  50. (61, 'Ed 312', 'Assessment of Teaching 2 ', 3, 'Ed 221'),
  51. (62, 'Ed 313', 'Principles of teaching', 3, 'Ed 224'),
  52. (63, 'FS 3', 'Micro-Teaching &the  use of technology', 1, 'FS 2'),
  53. (64, 'Eng 311', 'Arfo- Asian Literature ', 3, 'NONE '),
  54. (65, 'Eng 312', 'Introduction to Stylistics ', 3, 'NONE'),
  55. (66, 'Eng 313', 'Translating & Editing of  text ', 3, 'NONE'),
  56. (67, 'Eng 314', 'Teaching Literature', 3, 'NONE'),
  57. (68, 'Ed 314', ' Teaching Multigrade Class', 1, 'NONE'),
  58. (69, 'Ed 412', 'Use of popular Media ', 1, 'NONE'),
  59. (70, 'FS 5', 'Assestment of student Learning ', 1, 'FS 4'),
  60. (71, 'FS 6 ', 'Becoming a Teacher', 1, 'FS 5'),
  61. (72, 'Hum116', 'Art , Man & Society', 3, 'Hum 111'),
  62. (73, 'Eng 411', 'language & literature assestment', 3, 'NONE'),
  63. (74, 'Eng 412', 'lang. Curr. for sec. sch.', 3, 'NONE'),
  64. (75, 'Eng 413', 'Literary Criticism ', 3, 'NONE'),
  65. (76, 'Eng 414', 'Language Research ', 3, 'NONE'),
  66. (77, 'Eng 415', 'Dramatic & Stagecrafts', 3, 'NONE '),
  67. (78, 'CS 113', 'Basic Software Package w/ Internet Application', 3, 'None'),
  68. (79, 'Eng 111', 'Communication Arts', 3, 'None'),
  69. (80, 'Fil 111', 'Kom. sa Akad. Fil', 3, 'None'),
  70. (81, 'Math 1', 'Basic Math', 3, 'None'),
  71. (82, 'SCE 111', 'Earth Science', 3, 'None'),
  72. (83, 'Read 1', 'Dev. Reading 1', 3, 'None'),
  73. (84, 'Psych 116', 'General Psycology', 3, 'None'),
  74. (85, 'PE 111', 'Physical Fitness', 2, 'None'),
  75. (86, 'NSTP 111', 'Nat\'l Service Trng. Prog. 1', 3, 'None'),
  76. (87, 'Eng 211', 'Speech and Oral Communication', 3, 'Eng 121'),
  77. (88, 'Fil 211', 'Masining na Pagpapahayag', 3, 'Fil 121'),
  78. (89, 'Lit 121', 'World Literature', 3, 'Lit 111'),
  79. (90, 'SocSci 132', 'Rizal and Other Heroes', 3, 'None'),
  80. (91, 'Ed 211', 'Child Psycology', 3, 'Psych 116'),
  81. (92, 'Ed 212', 'Educational Technology 1', 3, 'None'),
  82. (93, 'FS 1', 'Observation of Learning Dev\'t and the School Environment', 1, 'None'),
  83. (94, 'Eng 212', 'The Teaching of Speaking', 3, 'Eng 121'),
  84. (95, 'Math 3', 'Elem. Theory of Numbers', 3, 'Math 2'),
  85. (96, 'PE 211', 'Team Sports', 2, 'PE 121'),
  86. (97, 'JEEP Start 1', ' ', 3, 'None'),
  87. (98, 'Stat 115', 'Intro. To Statistics', 3, 'None'),
  88. (99, 'Ed 311', 'The Teaching Profession', 3, 'Ed 221'),
  89. (100, 'Ed 312', 'Assessment of Student Lrng. 1', 3, 'Ed 221'),
  90. (101, 'Ed 313', 'Priciples of Teaching 2', 3, 'Ed 224'),
  91. (102, 'FS 3', 'Micro-Teaching and the use of Technology', 3, 'FS 2'),
  92. (103, 'SocSci 311', 'Basic Geography', 3, 'None'),
  93. (104, 'Chem 1', 'Basic Chemistry', 3, 'None'),
  94. (105, 'Fil 311', 'Pagpapahalagang Pampanitikan', 3, 'Fil 221'),
  95. (106, 'Math 5', 'Elementary Algebra', 3, 'None'),
  96. (107, 'Ed 314', 'Tchg. Multigrade Class', 1, 'Ed 221, Ed 222'),
  97. (108, 'Ed 412', 'Use of Popular Media', 1, 'None'),
  98. (109, 'FS 5', 'Assmt. of Study Learning', 1, 'FS 4'),
  99. (110, 'FS 6', 'Becoming a Teacher', 1, 'FS 5'),
  100. (111, 'Hum 116', 'Art, Man and Society', 3, 'Hum 111'),
  101. (112, 'Math 6', 'Mthds and Strat in Teaching Math', 3, 'Math 5'),
  102. (113, 'SocSci 411', 'Geog. and Nat Res of Phil', 3, 'SocSci311'),
  103. (114, 'HELE 111', 'Home Economic and Livinhood Eductaion', 3, 'None'),
  104. (115, 'FS PT', 'Practice Teaching(Whole Day)', 6, 'FS 6'),
  105. (116, 'Ed 320', 'Methods of Research', 3, 'Stat 115'),
  106. (117, 'Ed 321', 'Social Dimension of Educ.', 3, 'Ed 312'),
  107. (118, 'Ed 322', 'Assessment of Study Lrng.', 3, 'Ed 312'),
  108. (119, 'Ed 323', 'Environment Educ.', 1, 'None'),
  109. (120, 'FS 4', 'Team Teaching: Exploring the Curr.', 1, 'FS 3'),
  110. (121, 'Lit 321', 'Children\'s Literature', 3, 'None'),
  111. (122, 'Eng 321', 'Remedial Inst\'n in Eng', 3, 'None'),
  112. (123, 'Hum 111', 'Intro. to Humanities', 3, 'None'),
  113. (124, 'Econ 110', 'Prin. of Econ/AgRfm Tax and Coop', 3, 'None'),
  114. (125, 'Phys 1', 'Fundamental of Physics', 3, 'None'),
  115. (126, 'Edd 221', 'Facilitating Learning', 3, 'Ed 211'),
  116. (127, 'Ed 222', 'Educational Technology 2', 3, 'Ed 212'),
  117. (128, 'Ed 223', 'Curriculum Development', 3, 'None'),
  118. (129, 'FS 2', 'Classroom Mgt. Skill', 1, 'FS 1'),
  119. (130, 'Eng 221', 'Creative Writing', 3, 'Eng 121'),
  120. (131, 'Fil 221', 'Kontem Panitikang Fil', 3, 'None'),
  121. (132, 'Math 4', 'Plans and Solid Geometry', 3, 'Math 3'),
  122. (133, 'PE 221', 'Recreational Activities', 2, 'PE 211'),
  123. (134, 'JEEP Start 1', ' ', 3, 'None'),
  124. (135, 'Eng 121', 'Writing in the Disc.', 3, 'Eng 111'),
  125. (136, 'Fil 121', 'Pagbasa at Pagsulat', 3, 'Fil 111'),
  126. (137, 'Math 2', 'Contemporary Math', 3, 'Math 1'),
  127. (138, 'SCE 121', 'Survey of BioSci', 3, 'None'),
  128. (139, 'SCE 122', 'Astronomy', 3, 'None'),
  129. (140, 'Lit 111', 'Philippine Literature', 3, 'None'),
  130. (141, 'Read 2', 'Development Rdg. 2', 3, 'Read 1'),
  131. (142, 'PE 121', 'Rhythmic Activities', 2, 'PE 111'),
  132. (143, 'NSTP 121', 'Nat\'l Service Trang. Prog. 2', 3, 'NSTP 111'),
  133. (144, 'Eng 411', 'Lang. and Lit, Assessmet', 3, 'None'),
  134. (145, 'Eng 121', 'Writing in the disc.', 3, 'Eng 111'),
  135. (146, 'Eng 121', 'Writing in Disc.', 3, 'Eng 111'),
  136. (147, 'Fil 121', 'Pagbasa at Pagsulat', 3, 'Fil 111'),
  137. (148, 'Math 2', 'Contemporary Math', 3, 'Math 1'),
  138. (149, 'Sce 121', 'Survey of BioSci', 3, 'None'),
  139. (150, 'Econ 110', 'Prin. Econ/AgRfrm Tax Coop', 3, 'None'),
  140. (151, 'CS 113', 'Basic Software Pkge. w/ Internet Application', 3, 'None'),
  141. (152, 'Socio 115', 'Culture, Soc. and Fam w/ ARH', 3, 'None'),
  142. (153, 'PE 121', 'Rhythmic Activities', 2, 'PE 111'),
  143. (154, 'NSTP 121', 'Nat\'l. Service Trng. Prog. 2', 3, 'NSTP 111'),
  144. (155, 'Ed 221', 'Facilitating Learning', 3, 'Ed 211'),
  145. (156, 'Ed 222', 'Educational Technology 2', 3, 'Ed 212'),
  146. (157, 'Ed 223', 'Curriculum Development', 3, 'None'),
  147. (158, 'Ed 224', 'Principles of Teaching 1', 3, 'None'),
  148. (159, 'FS 2', 'Classroom Mgt. Skills', 1, 'FS 1'),
  149. (160, 'Eng 221', 'Intro. to Linguistics', 3, 'None'),
  150. (161, 'Eng 222', 'Campus Journalism', 3, 'None'),
  151. (162, 'Eng 223', 'Creative Writing', 3, 'None'),
  152. (163, 'Eng 224', 'Eng and Americal Lit.', 3, 'None'),
  153. (164, 'PE 221', 'Recreational Activities', 2, 'PE 211'),
  154. (165, 'Ed 320', 'Methods of Research', 3, 'Stat 115'),
  155. (166, 'Ed 322', 'Assessment of Stud. Lrng 2', 3, 'Ed 312'),
  156. (167, 'FS 4', 'Team Tchng: Exploring the Curr.', 1, 'FS 3'),
  157. (168, 'Ed 321', 'Social Dimension of Educ. ', 3, 'Ed 311'),
  158. (169, 'Ed 323', 'Environment Educ.', 1, 'None'),
  159. (170, 'Eng 321', 'Remedial Instr\'n in Eng', 3, 'None'),
  160. (171, 'Eng 323', 'Structure of English', 3, 'None'),
  161. (172, 'Eng 324', 'Mythology and Folklore', 3, '3'),
  162. (173, 'Eng 325', 'Eng for Specific Purpose', 3, 'None'),
  163. (174, 'Eng 322', 'Prep. and Evaluation of Instructional Materials', 3, 'None'),
  164. (175, 'FS PT', 'Practice Teaching(Whole Day)', 6, 'FS 5, FS 6'),
  165. (176, 'Eng 111', 'Communication skills', 3, 'None'),
  166. (177, 'Fil 111', 'Komunikasyon sa akademikong Pilipino', 3, 'None'),
  167. (178, 'Math 111', 'College algebra ', 3, 'None'),
  168. (179, 'CS 111', 'Basic software Package & Internet Application ', 3, 'None '),
  169. (180, 'Acctg 201', 'Fundamentals of Accounting 1', 3, 'None'),
  170. (181, 'PE 111', 'physical Fitness', 2, 'None '),
  171. (182, 'NSTP 111', 'national Service trng. Prog', 3, 'None'),
  172. (183, 'Hum 111', 'Art Man and Society', 3, 'None '),
  173. (184, 'Econ 111', 'Intro. To Economics w/ LRT', 3, 'None'),
  174. (185, 'Psycho 111', 'General Psychology', 3, 'None'),
  175. (186, 'Math 117', 'Calculus for Business', 3, 'math 111'),
  176. (187, 'Acctg 211', 'Financial Acctg & Reporting ', 3, 'Acctg 202'),
  177. (188, 'Hist 111', 'Phil History w/ politics and gov\'t', 3, 'None'),
  178. (189, 'Eng 113', 'Speech and oral com.', 3, 'Eng 112'),
  179. (190, 'PE 113', 'Team Sports ', 2, 'PE 111'),
  180. (191, 'Eng 114', 'Technical writng ', 3, 'Eng 113'),
  181. (192, 'Mgt 211', 'Human Behavior in Organization ', 3, 'Mgt 111'),
  182. (193, 'Cost 211', 'Cost Acctg & Cost Mgt. 1', 3, 'Acctg 212'),
  183. (194, 'Econ 114', 'MicroEconomic theory & Prac.', 3, 'Econ 112'),
  184. (195, 'CS 212', 'Funfd. of info. sys. Dev\'t', 3, 'CS 211'),
  185. (196, 'Law 211', 'law on Obiligation & contracts', 3, 'None'),
  186. (197, 'Pol Sci 211', 'Good Governance & Social Resp', 3, 'Hist 111'),
  187. (198, 'Math 211', 'Quantitative Techniques in Business', 3, 'Stat 113'),
  188. (199, 'Econ 114', 'Management Economics ', 3, 'Econ 113'),
  189. (200, 'Entrep 212', 'Phil. business enviroment', 3, 'Econ 113'),
  190. (201, 'Law 213', 'Law on sales, labor & other Comi. laws', 3, 'Law 212'),
  191. (202, 'Net Sci 112', 'Physical Science ', 3, 'Net sci 113'),
  192. (203, 'Entrep 213', 'Entrepreneurial Business Mgt', 3, 'Entrep 211'),
  193. (204, 'Soc Sci 111', 'Life & Works of Rizal', 3, 'None'),
  194. (205, 'Bre 212', 'Business Research 11', 3, 'bre 111'),
  195. (206, 'Eng1 112', 'Writing in the Dicipline ', 3, 'Eng 111'),
  196. (207, 'Fil 112', 'Pagbasa at Pagsulat tungo sa pananaliksik', 3, 'Fil 111'),
  197. (208, 'Math 113', 'Mathematics of investment', 3, 'Math 111'),
  198. (209, 'CS 211', 'fund. Programming & database theory & appli.', 3, 'CS 111'),
  199. (210, 'Mgt 111', 'Principles of mgt. & ORaganization ', 3, 'None '),
  200. (211, 'Acctg 202', 'Fundimentals of Accounting  11', 3, 'Acctg 201'),
  201. (212, 'PE 112', 'Rhythmic Activities ', 2, 'PE 111'),
  202. (213, 'NSTP 112', 'Nat.Service trng Prog. 2', 3, 'NSTP 111'),
  203. (214, 'NSTP 112', 'Nat.Service trng Prog. 2', 3, 'NSTP 111'),
  204. (215, 'Mktg', 'Principles of Marketing', 3, 'Mgt111'),
  205. (216, 'Econ112', 'Microeconomics Theory & Practice', 3, 'Econ111'),
  206. (217, 'Philos111', 'Philosophy, Values Ed. & works Ethics', 3, 'None'),
  207. (218, 'Stat 113', 'Business Stat w/ Comp. App.', 3, 'Math 113'),
  208. (219, 'Acctg 212', 'Financial Accounting and Report II', 6, 'Acctg 211'),
  209. (220, 'Lit 111', 'Philippine Literature', 3, 'Eng 112'),
  210. (221, 'Nat Sci', 'Siological Science', 3, 'none'),
  211. (222, 'PE 114', 'Recreational Activities', 2, 'PE 111'),
  212. (223, 'Engl 114', 'Technical Writing', 3, 'Engl 113'),
  213. (224, 'Cost  211', 'Cost Acctg & Cost Mgt 1', 3, 'Acctg 212'),
  214. (225, 'Mgt 211', 'Human Behavioral in Organization', 3, 'Mgt 111'),
  215. (226, 'Econ 114', 'Macroeconomics Theory & PracticeT', 3, 'Econ 112'),
  216. (227, 'CS 112', 'Fund. Of Info. Sys. & Sys. Devt.', 3, 'CS 211'),
  217. (228, 'Law 211', 'Law of Obligation & Contracts', 3, 'None'),
  218. (229, 'Pol Sci 211', 'Good Governance & Social Resp.', 3, 'Hist 111'),
  219. (230, 'Math 211', 'Quantitative Techniques in Business', 3, 'Hist 111'),
  220. (231, 'CS 213', 'Accounting Information System', 3, 'CS 212'),
  221. (232, 'Bre 111', 'Business Research 1', 3, 'None'),
  222. (233, 'Mgt 212', 'Mgt. Planning & Central Operation', 3, 'Mgt 111'),
  223. (234, 'Entrep 211', 'Industrial Relations', 3, 'Mgt 211'),
  224. (235, 'Law 212', 'Law on Business organization', 3, 'Law 211'),
  225. (236, 'Tax 211', 'Income Taxation', 3, 'Acctg 212'),
  226. (237, 'Mgt 213', 'Production and opreations Mgt.', 3, 'Stat 113'),
  227. (238, 'Cost 212', 'Cost Acctg & Cost Mgt. II', 3, 'Cost 211'),
  228. (239, 'Econ 114', 'Management Economics', 3, 'Econ 113'),
  229. (240, 'Entrep 212', 'Phil. Business environment', 3, 'Entrep 211'),
  230. (241, 'Law 213', 'Law on sales, labor and other Coml. Laws', 3, 'Law 212'),
  231. (242, 'Net Sci 112', 'Physical Science', 3, 'Net Sci 111'),
  232. (243, 'Entrep 213', 'Entrepreneurial Business Mgt.', 3, 'Entrep 211'),
  233. (244, 'Soc Sci 111', 'Life and Works of rizal', 3, 'None'),
  234. (245, 'Bre 212', 'Business Research II', 3, 'Bre 111'),
  235. (246, 'Socio 111', 'Society and culture with family planning', 3, 'none'),
  236. (247, 'Entrep 214', 'Sales management', 3, 'Entrep 213'),
  237. (248, 'Mgt 215', 'Business policies and formulation', 3, 'none'),
  238. (249, 'Tax 212', 'Business and Transfer Taxes', 3, 'Tax 2111'),
  239. (250, 'Entrep 215', 'Strategic marketing management', 3, 'Mktg 211'),
  240. (251, 'OJT 211', 'On job training (200 hours)', 3, 'None'),
  241. (252, 'Eng 111', 'Communication Arts 1', 3, 'None'),
  242. (253, 'Fil 111', 'Komunikasyon Sa akademikong Filipino ', 3, 'None'),
  243. (254, 'Math 111', 'College Algebra', 3, 'None'),
  244. (255, 'Psycho 111', 'Gen & Criminal Psychology w/ TVET', 3, 'None '),
  245. (256, 'Crim 211', 'Intro to Criminology', 3, 'None'),
  246. (257, 'Hist 111', 'Phillippine History', 3, 'None'),
  247. (258, 'SMGT 111', 'Security Mgt. and Service', 3, 'None '),
  248. (259, 'PE 111', 'Fundamentals of Matial Arts', 2, 'None'),
  249. (260, 'NSTP 111', 'Military Science 11', 3, 'None'),
  250. (261, 'Eng 113', 'Technical Report Writing ', 3, 'Eng 112'),
  251. (262, 'LEA 213', 'Industrial Sec. Mgt w/ TVET', 3, 'None '),
  252. (263, 'CLJ 211', 'Criminal Law (Book 1)', 3, 'None'),
  253. (264, 'Socio 111', 'Society & Culture w/ Pop Ed.', 3, 'None'),
  254. (265, 'Econ 111', 'Basic Economics w/ TAR', 3, 'None '),
  255. (266, 'Crimtic 211', 'Personnel Idenfication w/ TVET ', 3, 'None'),
  256. (267, 'CS 111', 'Basic Software Pkcg Int. Application', 3, 'None'),
  257. (268, 'FIl 113', 'Masining na pagpapahayag', 3, 'Fil 112'),
  258. (269, 'PE 211', 'First Aid & water Survival ', 2, 'PE 121'),
  259. (278, 'CDI 215', 'Court Testimony', 3, 'CLJ 211'),
  260. (279, 'CDI 217', 'Fire Tech & Arson Investigation ', 3, 'None'),
  261. (280, 'LEA 217', 'Police Intellegence', 3, 'None'),
  262. (281, 'LEA 218', 'International Policing ', 3, 'None'),
  263. (282, 'PerDev 111', 'Personality Development ', 3, 'None'),
  264. (283, 'Crim 215', 'Criminology  Research & Stat', 3, 'None '),
  265. (284, 'CLJ 214', 'Criminal Evidence ', 3, 'CLJ 211'),
  266. (285, 'Review 211', 'Refresher Evidence 1', 3, 'None'),
  267. (286, 'Eng 112', 'Speech and oral Com.', 3, 'Eng 111'),
  268. (287, 'Fil 120', 'Pagbasa at pagsulat tungo sa pananaliksik', 3, 'Fil 111'),
  269. (288, 'Math 121', 'Plane trigonometry', 3, 'Math 111'),
  270. (289, 'LEA 211', 'PHIL Criminal justice', 3, 'Crim 211'),
  271. (290, 'LEA 212', 'Police or. and administration', 2, 'none'),
  272. (291, 'Ethics 111', 'Ethics and values', 3, 'none'),
  273. (292, 'Pol Sci 111', 'Pol, Sci and Phil. Constitution', 3, 'none'),
  274. (293, 'Hum 111', 'Logic ', 3, 'none'),
  275. (294, 'PE 112', 'Disarming technique', 2, 'PE 111'),
  276. (295, 'NSTP 121', 'Military Science 12', 3, 'MS 11'),
  277. (296, 'Eng 113', 'Technical report wriring', 3, 'English 112'),
  278. (297, 'LEA 213', 'Industrial Sec. Mgt with TVET', 3, 'None'),
  279. (298, 'CLJ 211', 'Criminal Law (Book 1)', 3, 'none'),
  280. (300, 'Econ 111', 'Basic economics', 2, 'none'),
  281. (301, 'Crimtic 211', 'Basic Software pckg int App', 2, 'none'),
  282. (302, 'Fil 113', 'Masining na paglalayag', 3, 'Fil 112'),
  283. (303, 'PE 211', 'First Aid and water survival', 2, 'PE 211'),
  284. (304, 'Eng 114', 'Technical report writing 2', 3, 'none'),
  285. (305, 'Crim 213', 'Juvenile Deliquency and crime prevention', 3, 'none'),
  286. (306, 'CDI 2111', 'Police patrol operation', 3, 'none'),
  287. (307, 'Crimtic 212', 'Poloice photography', 3, 'none'),
  288. (308, 'LEA 215', 'Fund of criminal investigation', 3, 'none'),
  289. (309, 'SMGT 112', 'Security Mgt Services', 3, 'none'),
  290. (310, 'PE 114', 'Marmanship and combat shooting', 2, 'none'),
  291. (311, 'OJT', 'On job training', 2, 'none'),
  292. (312, 'LEA 214', 'Police Comparative System', 3, 'none'),
  293. (313, 'LEA 216', 'Police personnel and record mgt.', 3, 'none'),
  294. (314, 'CLJ 212', 'Criminal law (Book 2)', 3, 'none'),
  295. (315, 'Chem 111', 'General Chemistry', 3, 'none'),
  296. (316, 'CDI 212', 'traffic Mgt. and accident investigation', 2, 'none'),
  297. (317, 'Crimtic 213', 'Forensic ballistic', 2, 'none'),
  298. (318, 'Crimtic 214', 'Questioned Document', 3, 'none'),
  299. (319, 'CDI 211', 'Institutional Correction', 3, 'none'),
  300. (320, 'Crimtic 215', 'Polgraphy ', 3, 'none'),
  301. (321, 'LEA 219', 'Police Planning', 3, 'none'),
  302. (322, 'CLJ 213', 'Criminal procedure', 3, 'CLJ 212'),
  303. (323, 'Chem 211', 'Forensic chem and toxiclology', 3, 'Chem 111'),
  304. (324, 'CDI 216', 'White collar crime', 3, 'none'),
  305. (325, 'CDI 214', 'Organized crime', 3, 'none'),
  306. (326, 'Crimtic 216', 'Legal medicine', 3, 'none'),
  307. (327, 'CA 212', 'Non Institutional Correction', 3, 'CA  212'),
  308. (328, 'Prac 1 & 2', 'ON JOB TRAINING', 6, 'none'),
  309. (329, 'Review 211', 'Refreseher Course', 3, 'none'),
  310. (330, 'FTS 11', 'Field trip seminar', 3, 'none');

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in c#. navigate_createform

Step 2

Do the form just like shown below. navigate_setorm

Step 3

Go to the code editor. After that, add a namespace to access MySQL libraries.
  1. using MySql.Data.MySqlClient;

Step 4

Create a connection between C# and MySQL Database. After that, declare all the classes and variables that are needed.
  1.         MySqlConnection con = new MySqlConnection("server=localhost;user id=root;password=;database=dbsubjects;sslMode=none");
  2.         MySqlCommand cmd;
  3.         MySqlDataAdapter da;
  4.         DataTable dt;
  5.         String sql;
  6.         int maxrow;
  7.         int inc_value;
  8.         int resultPerPage = 10;
  9.         int startResult;

Step 5

Create a method to retrieve a single result in the MySQL database.
  1.         private void loadDataSingle(String sql)
  2.         {
  3.             try
  4.             {
  5.                 con.Open();
  6.                 cmd = new MySqlCommand();
  7.                 cmd.Connection = con;
  8.                 cmd.CommandText = sql;
  9.                 da = new MySqlDataAdapter();
  10.                 da.SelectCommand = cmd;
  11.                 dt = new DataTable();
  12.                 da.Fill(dt);
  13.                 maxrow = dt.Rows.Count - 1;
  14.             }
  15.             catch (Exception ex)
  16.             {
  17.                 MessageBox.Show(ex.Message);
  18.             }
  19.             finally
  20.             {
  21.                 con.Close();
  22.                 da.Dispose();
  23.             }
  24.  
  25.         }

Step 6

Create a method to retrieve the list of data in the MySQL database.
  1.         private void loadDataList(String sql,DataGridView dtg)
  2.         {
  3.             try
  4.             {
  5.                 con.Open();
  6.                 cmd = new MySqlCommand();
  7.                 cmd.Connection = con;
  8.                 cmd.CommandText = sql;
  9.                 da = new MySqlDataAdapter();
  10.                 da.SelectCommand = cmd;
  11.                 dt = new DataTable();
  12.                 da.Fill(dt);
  13.  
  14.                 dtg.DataSource = dt;
  15.             }catch(Exception ex)
  16.             {
  17.                 MessageBox.Show(ex.Message);
  18.             }
  19.             finally
  20.             {
  21.                 con.Close();
  22.                 da.Dispose();
  23.             }
  24.          
  25.         }

Step 7

Create a method for the previous function.
  1.         private void goto_Previous()
  2.         {
  3.             if (inc_value == 0)
  4.             {
  5.                 return;
  6.             }
  7.             else if(inc_value > 0)
  8.             {
  9.                 inc_value = inc_value - 1;
  10.                 startResult = inc_value * resultPerPage;
  11.             }
  12.  
  13.             sql = "Select * from subject limit " + startResult + "," + resultPerPage + "";
  14.             loadDataList(sql, dtg_list);
  15.  
  16.         }

Step 8

Create a method for the next function.
  1.         private void goto_Next()
  2.         {
  3.  
  4.             if (inc_value != maxrow  - 2)
  5.             {
  6.                 inc_value = inc_value + 1;
  7.                 startResult = inc_value  * resultPerPage;
  8.             }
  9.             else
  10.             {
  11.                 if (inc_value == maxrow)
  12.                 {
  13.                     return;
  14.                 }
  15.             }
  16.             sql = "Select * from subject limit " + startResult + "," + resultPerPage + "";
  17.             loadDataList(sql, dtg_list);
  18.  
  19.         }

Step 9

Double click the form and do the following codes for getting the total number rows of the table and limit the data to be displayed in the datagridview.
  1.             sql = "Select * from subject";
  2.             loadDataSingle(sql);
  3.  
  4.             sql = "Select * from subject limit 10";
  5.             loadDataList(sql, dtg_list);

Step 10

Double click the “Previous” button and call the method for the previous.
  1. goto_Previous();

Step 11

Double click the “Next” button and call the method for the next.
  1.  
  2. goto_Next();
Note: The database file is included inside the folder. The complete source code is included. You can download it and run it on your computer For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below.

Add new comment