• Register now to get access to thousands of Tutorials, Leaked content, Hot NSFW and much more. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

How to Load Data in the Listview Using C# and MySQL Database

pSparow

Data Miner
P Rep
0
0
0
Rep
0
P Vouches
0
0
0
Vouches
0
Posts
80
Likes
195
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
The Listview control is used to display a collection of items. This also provides many ways to arrange and display items with item text and an icon (optionally) is used to determine the type of item. So, now I’m going to teach you how to load data in the ListView using C# and MySQL Database. This is just a simple program but I’m pretty sure that this method will help you when you encounter problem of displaying set of data in the database and can be displayed in the Listview. So let’s get started.
Creating Database

Create a database and named it “salesdb”;
Execute the following query to add the table with data in the database.
  1. CREATE

    TABLE

    `tblsales`

    (
  2. `SalesId`

    int

    (

    11

    )

    NOT

    NULL

    ,
  3. `TRANSDATE`

    date

    NOT

    NULL

    ,
  4. `Product`

    varchar

    (

    90

    )

    NOT

    NULL

    ,
  5. `TotalSales`

    double

    NOT

    NULL


  6. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

  7. --
  8. -- Dumping data for table `tblsales`
  9. --

  10. INSERT

    INTO

    `tblsales`

    (

    `SalesId`

    ,

    `TRANSDATE`

    ,

    `Product`

    ,

    `TotalSales`

    )

    VALUES


  11. (

    1

    ,

    '2018-01-30'

    ,

    'Cellphone'

    ,

    1400

    )

    ,
  12. (

    2

    ,

    '2018-02-28'

    ,

    'Laptop'

    ,

    800

    )

    ,
  13. (

    3

    ,

    '2018-03-31'

    ,

    'Desktop'

    ,

    5052

    )

    ,
  14. (

    4

    ,

    '2019-04-30'

    ,

    'Ipod'

    ,

    8030

    )

    ,
  15. (

    5

    ,

    '2019-05-31'

    ,

    'Tablet'

    ,

    10000

    )

    ;

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application.
csharp_create_app_10.png

Step 2

Add a ListView inside the Form and do the Form just like this.
form_listview.png

Step 3

Open the code editor and add a namespace to access MySQL Library.
  1. using

    MySql.Data.MySqlClient

    ;

Step 4

Create a method for retrieving data in the database to be displayed in the ListView.
  1. private

    void

    LoadData(

    )
  2. {
  3. MySqlConnection con =

    new

    MySqlConnection(

    "server=localhost;user id=root;password=;database=salesdb;sslMode=none"

    )

    ;
  4. MySqlCommand cmd;
  5. MySqlDataAdapter da;
  6. DataTable dt;


  7. try
  8. {
  9. con.

    Open

    (

    )

    ;
  10. cmd =

    new

    MySqlCommand(

    )

    ;
  11. cmd.

    Connection

    =

    con;
  12. cmd.

    CommandText

    =

    "Select * FROM tblsales"

    ;
  13. da =

    new

    MySqlDataAdapter(

    )

    ;
  14. da.

    SelectCommand

    =

    cmd;
  15. dt =

    new

    DataTable(

    )

    ;
  16. da.

    Fill

    (

    dt)

    ;

  17. for

    (

    int

    i =

    0

    ;

    i <

    dt.

    Rows

    .

    Count

    ;

    i++

    )
  18. {
  19. DataRow dr =

    dt.

    Rows

    [

    i]

    ;
  20. ListViewItem listitem =

    new

    ListViewItem(

    dr[

    "SalesId"

    ]

    .

    ToString

    (

    )

    )

    ;
  21. listitem.

    SubItems

    .

    Add

    (

    dr[

    "TRANSDATE"

    ]

    .

    ToString

    (

    )

    )

    ;
  22. listitem.

    SubItems

    .

    Add

    (

    dr[

    "Product"

    ]

    .

    ToString

    (

    )

    )

    ;
  23. listitem.

    SubItems

    .

    Add

    (

    dr[

    "TotalSales"

    ]

    .

    ToString

    (

    )

    )

    ;
  24. listView1.

    Items

    .

    Add

    (

    listitem)

    ;
  25. }

  26. }
  27. catch

    (

    Exception ex)
  28. {
  29. MessageBox.

    Show

    (

    ex.

    Message

    )

    ;
  30. }
  31. finally
  32. {
  33. con.

    Close

    (

    )

    ;
  34. }
  35. }

Step 5

Call the method that you have created and put it inside the Load event handler

to perform in the first load of the Form
  1. private

    void

    Form1_Load(

    object

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

    )

    ;
  4. }

 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

452,499

349,924

349,934

Top