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

The Simple Way to Navigate Records Based On ListView in C#

0hSally

Grey Hat
0 Rep
0
0
0
Rep
0
0 Vouches
0
0
0
Vouches
0
Posts
71
Likes
168
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
This time, I will teach you how to navigate records based on ListView in C# and MySQL Database. This method has the ability to navigate records in the ListView. It also provides a next and previous button that allows the movement of records back and forth. In this way, you can control the data to be displayed in the listview.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.
creating_application_c_3.png

Step 2

Do the form just like shown below.
nav1.png

Step 3

Press F7 to open the code editor. In the code editor, add a namespace to access MySQL

libraries

  1. using

    MySql.Data.MySqlClient

    ;

Step 4

Establish 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 for retrieving single data in the database.

  1. private

    void

    loadSingleResult(

    String

    sql)
  2. {
  3. try
  4. {
  5. con.

    Open

    (

    )

    ;

  6. cmd =

    new

    MySqlCommand(

    )

    ;
  7. da =

    new

    MySqlDataAdapter(

    )

    ;
  8. dt =

    new

    DataTable(

    )

    ;

  9. cmd.

    Connection

    =

    con;
  10. cmd.

    CommandText

    =

    sql;
  11. da.

    SelectCommand

    =

    cmd;
  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. }


Step 6

Create a method for retrieving list of data in the database

  1. private

    void

    loadResultList(

    String

    sql, ListView lst)
  2. {
  3. try
  4. {
  5. con.

    Open

    (

    )

    ;

  6. cmd =

    new

    MySqlCommand(

    )

    ;
  7. da =

    new

    MySqlDataAdapter(

    )

    ;
  8. dt =

    new

    DataTable(

    )

    ;

  9. cmd.

    Connection

    =

    con;
  10. cmd.

    CommandText

    =

    sql;
  11. da.

    SelectCommand

    =

    cmd;
  12. da.

    Fill

    (

    dt)

    ;

  13. lst.

    Items

    .

    Clear

    (

    )

    ;
  14. foreach

    (

    DataRow r in

    dt.

    Rows

    )
  15. {
  16. var

    list =

    lst.

    Items

    .

    Add

    (

    r.

    Field

    <

    string

    >

    (

    1

    )

    .

    ToString

    (

    )

    )

    ;
  17. list.

    SubItems

    .

    Add

    (

    r.

    Field

    <

    string

    >

    (

    2

    )

    .

    ToString

    (

    )

    )

    ;
  18. list.

    SubItems

    .

    Add

    (

    r.

    Field

    <

    int

    >

    (

    3

    )

    .

    ToString

    (

    )

    )

    ;
  19. list.

    SubItems

    .

    Add

    (

    r.

    Field

    <

    string

    >

    (

    4

    )

    .

    ToString

    (

    )

    )

    ;
  20. }
  21. }
  22. catch

    (

    Exception ex)
  23. {
  24. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  25. }
  26. finally
  27. {
  28. con.

    Close

    (

    )

    ;
  29. da.

    Dispose

    (

    )

    ;
  30. }

  31. }

Step 7

Create a method for the next records.
  1. private

    void

    next_record(

    )
  2. {

  3. if

    (

    inc_value !=

    maxrow -

    2

    )
  4. {
  5. inc_value =

    inc_value +

    1

    ;
  6. startResult =

    inc_value *

    resultPerPage;
  7. }
  8. else
  9. {
  10. if

    (

    inc_value ==

    maxrow)
  11. {
  12. return

    ;
  13. }
  14. }
  15. sql =

    "Select * from subject limit "

    +

    startResult +

    ","

    +

    resultPerPage +

    ""

    ;
  16. loadResultList(

    sql, listView1)

    ;

  17. }

Step 8

Create a method for the previous records
  1. private

    void

    previous_record(

    )
  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. sql =

    "Select * from subject limit "

    +

    startResult +

    ","

    +

    resultPerPage +

    ""

    ;
  13. loadResultList(

    sql, listView1)

    ;

  14. }

Step 9

Write the following codes for retrieving and displaying data in the first load of the form.
  1. private

    void

    Form1_Load(

    object

    sender, EventArgs e)
  2. {
  3. sql =

    "Select * from subject"

    ;
  4. loadSingleResult(

    sql)

    ;

  5. sql =

    "Select * from subject limit 10"

    ;
  6. loadResultList(

    sql, listView1)

    ;
  7. }

Step 10

Write the following codes for the next button
  1. private

    void

    btn_next_Click(

    object

    sender, EventArgs e)
  2. {
  3. next_record(

    )

    ;
  4. }

Step 11

Write the following codes for the previous button
  1. private

    void

    btn_previous_Click(

    object

    sender, EventArgs e)
  2. {
  3. previous_record(

    )

    ;
  4. }

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.


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

452,292

323,666

323,675

Top