• We just launched and are currently in beta. Join us as we build and grow the community.

How to Create an Advance Record Navigation in C#

Drizzydrane

Meme Machine
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
52
Likes
160
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
This tutorial is a continuation of our last topic called “How to Create a Simple Record Navigation in C#”. But at this moment, we will modify our code and add more additional control such as First Record, Last Record and No of Record. And this look like as shown below.
d1_1.png


To start building with this application, let’s open first our last project called “student_info”. Then add Buttons and Label. Next, arrange and Design it the same as shown above figure.
This time we will add functionality to our four buttons such as First, Previous, Next and Last. To start with, double click the “First record” button. Then add the following code:
  1. private

    void

    btnfirst_Click(

    object

    sender, EventArgs e)
  2. {
  3. //check if the current record is not equal to zero
  4. if

    (

    curRecord !=

    0

    )
  5. //set the current record to zero
  6. curRecord =

    0

    ;
  7. //call the Navaigate sub procedure
  8. Navigate(

    )

    ;
  9. //set the record value
  10. lblrecord.

    Text

    =

    curRecord +

    1

    +

    " of "

    +

    totRecord;

  11. }

Then for the “Previous Record” Button. Add the following code:
  1. private

    void

    btnprev_Click(

    object

    sender, EventArgs e)
  2. {
  3. //check if the current record is greater than zero
  4. if

    (

    curRecord >

    0

    )
  5. {
  6. //then decrement the current record by one
  7. curRecord =

    curRecord -

    1

    ;

  8. //call the Navaigate sub procedure
  9. Navigate(

    )

    ;
  10. //set the record value
  11. lblrecord.

    Text

    =

    curRecord +

    1

    +

    " of "

    +

    totRecord;
  12. }

  13. }

For the “Next Record” Button. Add the following code:
  1. private

    void

    btnnext_Click(

    object

    sender, EventArgs e)
  2. {
  3. //check if the current record is not equal to the total Record minus by one
  4. if

    (

    curRecord !=

    totRecord -

    1

    )
  5. {
  6. //increment the current record by one
  7. curRecord =

    curRecord +

    1

    ;
  8. //call the Navaigate sub procedure
  9. Navigate(

    )

    ;
  10. //set the record value
  11. lblrecord.

    Text

    =

    curRecord +

    1

    +

    " of "

    +

    totRecord;
  12. }
  13. }

And lastly for “Last Record” Button. Here’s the following code:
  1. private

    void

    btnlast_Click(

    object

    sender, EventArgs e)
  2. {
  3. //check if the current Record is not Equal to the total record
  4. if

    (

    curRecord !=

    totRecord)
  5. //set the current record equal to total record
  6. curRecord =

    totRecord -

    1

    ;

  7. //call the Navaigate sub procedure
  8. Navigate(

    )

    ;
  9. //set the record value
  10. lblrecord.

    Text

    =

    curRecord +

    1

    +

    " of "

    +

    totRecord;
  11. }

At this time, you now test the application by pressing the “F5” or the Start button.

And here’s all the code used for this application.
  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.Windows.Forms

    ;
  9. using

    System.Data.OleDb

    ;
  10. namespace

    student_info
  11. {
  12. public

    partial

    class

    Form1 :

    Form
  13. {
  14. //declare new variable named dt as New Datatable
  15. DataTable dt =

    new

    DataTable(

    )

    ;
  16. //this line of code used to connect to the server and locate the database (usermgt.mdb)
  17. static

    string

    connection =

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "

    +

    Application.

    StartupPath

    +

    "/studentdb.mdb"

    ;
  18. OleDbConnection conn =

    new

    OleDbConnection(

    connection)

    ;

  19. int

    curRecord =

    0

    ;
  20. int

    totRecord =

    0

    ;

  21. public

    Form1(

    )
  22. {
  23. InitializeComponent(

    )

    ;
  24. }

  25. private

    void

    btnload_Click(

    object

    sender, EventArgs e)
  26. {
  27. //create a new datatable
  28. dt =

    new

    DataTable(

    )

    ;
  29. //create our SQL SELECT statement
  30. string

    sql =

    "Select * from tblstudent"

    ;
  31. //then we execute the SQL statement against the Connection using OleDBDataAdapter
  32. OleDbDataAdapter da =

    new

    OleDbDataAdapter(

    sql, conn)

    ;
  33. //we fill the result to dt which declared above as datatable
  34. da.

    Fill

    (

    dt)

    ;

  35. //set the curRecord to zero
  36. curRecord =

    0

    ;
  37. //get the total number of record available in the database and stored to totRecord Variable
  38. totRecord =

    dt.

    Rows

    .

    Count

    ;


  39. //then we populate the datagridview by specifying the datasource equal to dt
  40. dataGridView1.

    DataSource

    =

    dt;

  41. }

  42. private

    void

    dataGridView1_CellClick(

    object

    sender, DataGridViewCellEventArgs e)
  43. {
  44. //it checks if the row index of the cell is greater than or equal to zero
  45. if

    (

    e.

    RowIndex

    >=

    0

    )
  46. {
  47. //gets a collection that contains all the rows
  48. DataGridViewRow row =

    this

    .

    dataGridView1

    .

    Rows

    [

    e.

    RowIndex

    ]

    ;
  49. //populate the textbox from specific value of the coordinates of column and row.
  50. txtid.

    Text

    =

    row.

    Cells

    [

    0

    ]

    .

    Value

    .

    ToString

    (

    )

    ;
  51. txtfname.

    Text

    =

    row.

    Cells

    [

    1

    ]

    .

    Value

    .

    ToString

    (

    )

    ;
  52. txtlname.

    Text

    =

    row.

    Cells

    [

    2

    ]

    .

    Value

    .

    ToString

    (

    )

    ;
  53. comboBox1.

    Text

    =

    row.

    Cells

    [

    3

    ]

    .

    Value

    .

    ToString

    (

    )

    ;
  54. txtgender.

    Text

    =

    row.

    Cells

    [

    4

    ]

    .

    Value

    .

    ToString

    (

    )

    ;
  55. txtaddress.

    Text

    =

    row.

    Cells

    [

    5

    ]

    .

    Value

    .

    ToString

    (

    )

    ;

  56. }

  57. }

  58. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  59. {
  60. loadDatatoCombobox(

    )

    ;
  61. }
  62. private

    void

    loadDatatoCombobox(

    )
  63. {

  64. //create a new datatable
  65. DataTable table =

    new

    DataTable(

    )

    ;
  66. //create our SQL SELECT statement
  67. string

    sql =

    "Select course from tblstudent"

    ;
  68. try
  69. {
  70. conn.

    Open

    (

    )

    ;
  71. //then we execute the SQL statement against the Connection using OleDBDataAdapter
  72. OleDbDataAdapter da =

    new

    OleDbDataAdapter(

    sql, conn)

    ;
  73. //we fill the result to dt which declared above as datatable
  74. da.

    Fill

    (

    table)

    ;
  75. //we add new entry to our datatable manually
  76. //becuase Select Course is not Available in the Database
  77. table.

    Rows

    .

    Add

    (

    "Select Course"

    )

    ;
  78. //set the combobox datasource
  79. comboBox1.

    DataSource

    =

    table;
  80. //choose the specific field to display
  81. comboBox1.

    DisplayMember

    =

    "course"

    ;
  82. comboBox1.

    ValueMember

    =

    "course"

    ;
  83. //set default selected value
  84. comboBox1.

    SelectedValue

    =

    "Select Course"

    ;
  85. }
  86. catch

    (

    Exception ex)
  87. {
  88. //this will display some error message if something
  89. //went wrong to our code above during execution
  90. MessageBox.

    Show

    (

    ex.

    ToString

    (

    )

    )

    ;
  91. }

  92. }

  93. private

    void

    Navigate(

    )
  94. {
  95. txtid.

    Text

    =

    dt.

    Rows

    [

    curRecord]

    [

    0

    ]

    .

    ToString

    (

    )

    ;
  96. txtfname.

    Text

    =

    dt.

    Rows

    [

    curRecord]

    [

    1

    ]

    .

    ToString

    (

    )

    ;
  97. txtlname.

    Text

    =

    dt.

    Rows

    [

    curRecord]

    [

    2

    ]

    .

    ToString

    (

    )

    ;
  98. comboBox1.

    Text

    =

    dt.

    Rows

    [

    curRecord]

    [

    3

    ]

    .

    ToString

    (

    )

    ;
  99. txtgender.

    Text

    =

    dt.

    Rows

    [

    curRecord]

    [

    4

    ]

    .

    ToString

    (

    )

    ;
  100. txtaddress.

    Text

    =

    dt.

    Rows

    [

    curRecord]

    [

    5

    ]

    .

    ToString

    (

    )

    ;
  101. }

  102. private

    void

    btnprev_Click(

    object

    sender, EventArgs e)
  103. {
  104. //check if the current record is greater than zero
  105. if

    (

    curRecord >

    0

    )
  106. {
  107. //then decrement the current record by one
  108. curRecord =

    curRecord -

    1

    ;

  109. //call the Navaigate sub procedure
  110. Navigate(

    )

    ;
  111. //set the record value
  112. lblrecord.

    Text

    =

    curRecord +

    1

    +

    " of "

    +

    totRecord;
  113. }

  114. }

  115. private

    void

    btnnext_Click(

    object

    sender, EventArgs e)
  116. {
  117. //check if the current record is not equal to the total Record minus by one
  118. if

    (

    curRecord !=

    totRecord -

    1

    )
  119. {
  120. //increment the current record by one
  121. curRecord =

    curRecord +

    1

    ;
  122. //call the Navaigate sub procedure
  123. Navigate(

    )

    ;
  124. //set the record value
  125. lblrecord.

    Text

    =

    curRecord +

    1

    +

    " of "

    +

    totRecord;
  126. }
  127. }

  128. private

    void

    btnfirst_Click(

    object

    sender, EventArgs e)
  129. {
  130. //check if the current record is not equal to zero
  131. if

    (

    curRecord !=

    0

    )
  132. //set the current record to zero
  133. curRecord =

    0

    ;
  134. //call the Navaigate sub procedure
  135. Navigate(

    )

    ;
  136. //set the record value
  137. lblrecord.

    Text

    =

    curRecord +

    1

    +

    " of "

    +

    totRecord;

  138. }

  139. private

    void

    btnlast_Click(

    object

    sender, EventArgs e)
  140. {
  141. //check if the current Record is not Equal to the total record
  142. if

    (

    curRecord !=

    totRecord)
  143. //set the current record equal to total record
  144. curRecord =

    totRecord -

    1

    ;

  145. //call the Navaigate sub procedure
  146. Navigate(

    )

    ;
  147. //set the record value
  148. lblrecord.

    Text

    =

    curRecord +

    1

    +

    " of "

    +

    totRecord;
  149. }




  150. }
  151. }


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,135

324,143

Top