Daniel Rabinovits

Daniel Rabinovits

  • NA
  • 34
  • 7.7k

I am trying to populate a 3 column listView.

Aug 15 2019 9:20 AM
I am trying to populate a 3 column list box.  It populates the first item only into many columns.  
string connStr = Properties.Settings.Default.conn;
string sql = "SELECT Comment, [User Name], EventDate FROM Notes WHERE [Project ID] = " + value + " ORDER BY EventDate DESC;";
listView1.Columns.Add("Comment", listView1.Width / 2);
listView1.Columns.Add("User Name", listView1.Width / 4);
listView1.Columns.Add("Event Date", listView1.Width / 4);
using (OleDbConnection connection = new OleDbConnection(connStr))
{
   OleDbCommand command = new OleDbCommand(sql, connection);
   connection.Open();
   OleDbDataReader reader = command.ExecuteReader();
   if (reader.HasRows)
   {
      while (reader.Read())
      {
         ListViewItem lvi = new ListViewItem(reader[0].ToString());
         for (int i = 0; i <= reader.FieldCount - 1; i++)
         {
            lvi.SubItems.Add(reader[i].ToString());
         }
         listView1.Items.Add(lvi);
      }
      reader.Close();
      connection.Close();
      }
}
 
 

Answers (1)