1

I don't understand why the listview is empty.

enter image description here

enter image description here

Refresh button = Button2, and this is my code :

       private void button2_Click(object sender, EventArgs e)
    {

        MySqlDataAdapter ada = new MySqlDataAdapter("select * from Teams", _dbConnect.getConnection());
        DataTable dt = new DataTable();
        ada.Fill(dt);

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            ListViewItem listitem = new ListViewItem(dr["team_id"].ToString());
            listitem.SubItems.Add(dr["team_name"].ToString());
            listView1.Items.Add(listitem);
        }
    }
2
  • Step through your code and see what's going on. Are you sure your table has data to return? Commented May 11, 2015 at 12:36
  • I added a photo with my Table Commented May 11, 2015 at 12:36

3 Answers 3

2

You should add at least one column to your ListView. Add another column to display a sub-item.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

See improved answer. :)
It works, but how can i dynamicaly add headers for how many columns are in my db Table?
foreach (DataColumn col in dt.Columns) { listView1.Columns.Add(col.ColumnName); }
0

I don't believe you require subitems in this case, add the items directly;

listView1.Items.Add(dr["team_name"].ToString());

Comments

0

Try this code: Download and add reference to Mysql.Data dll file

using MySql.Data.MySqlClient;

 public static void fillmyList(ListView myListView, string query)
    {
        myListView.Items.Clear();
        myListView.Columns.Clear();
        string server = "localhost";
        string database = "mydatabaseName";
        string uid = "myUser";
        string password = "MyPassword";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        connection = new MySqlConnection(connectionString);
        try
        {
            //Open connection
            if (OpenConnection() == true)
            {
                //Create Command
                MySqlCommand cmd = new MySqlCommand(query, connection);
                //Create a data reader and Execute the command
                MySqlDataReader dataReader = cmd.ExecuteReader();
                //Read the data and store them in the listview
                if (dataReader.FieldCount > 0)
                {
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        if (i == 0)
                        {
                            myListView.Columns.Add(dataReader.GetName(0), 0, HorizontalAlignment.Left);
                        }
                        else
                        {
                            myListView.Columns.Add(dataReader.GetName(i).ToString().Replace("_", " "), 80, HorizontalAlignment.Left);
                        }
                    }

                    ListViewItem lv = new ListViewItem();
                    //
                    while (dataReader.Read())
                    {
                        lv = myListView.Items.Add(dataReader[dataReader.GetName(0)].ToString().Replace('_', ' '));
                        for (int h = 1; h < dataReader.FieldCount; h++)
                        {

                                lv.SubItems.Add(dataReader[dataReader.GetName(h)].ToString());
                        }
                    }
                }
                for (int i = 1; i < myListView.Columns.Count; i++)
                    myListView.Columns[i].Width = -2;
                //close Data Reader
                dataReader.Close();
                //close Connection
                CloseConnection();
                         }
        }
        catch (Exception)
        {
            //  MessageBox.Show(ex.Message);
        }


    }

      //open connection to database
    private static bool OpenConnection()
    {
        try
        {
            connection.Open();
            return true;
        }
        catch (MySqlException ex)
        {
            //When handling errors, you can your application's response based on the error number.
            //The two most common error numbers when connecting are as follows:
            //0: Cannot connect to server.
            //1045: Invalid user name and/or password.
            switch (ex.Number)
            {
                case 0:
                    MessageBox.Show("Cannot connect to server.  Contact administrator");
                    break;

                case 1045:
                    MessageBox.Show("Invalid username/password, please try again");
                    break;
            }
            return false;
        }
    }

    //Close connection
    private static bool CloseConnection()
    {
        try
        {
            connection.Close();
            return true;
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message);
            return false;
        }
    }

1 Comment

You can then call the fillmyList method as--- fillmyList (Llistview name,"select * from tablename ");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.