Jack Cheetham

Jack Cheetham

  • NA
  • 1
  • 2.5k

Populating a ListView with a DataTable

May 5 2015 10:24 AM
Hi,
 
can anyone help me? I'm trying to populate a ListView on a winForms application with a Data set which I am retrieving from a Database. The ListView gets populated when a button is clicked by a user.
 
Here is my code:
 
private void button2_Click_1(object sender, EventArgs e)
{
   {
      lstvw1.View = View.Details;
      string connectionString = null;
      string server = null;
      SqlConnection con;
      server = System.Environment.GetEnvironmentVariable("Corp Report Server");
      connectionString = "Data Source=" + server + ";Initial Catalog=Corp_Rpt;Integrated Security=True";
      con = new SqlConnection(connectionString);
      SqlDataAdapter da = new SqlDataAdapter("Select Top 5 * From Corp_Rpt.Common.VW_BOOKINGS", con);
      DataTable dt = new DataTable();
      lstvw1.Items.Clear();
      da.Fill(dt);
      foreach (DataRow dr in dt.Rows)
      {
         ListViewItem lvi = new ListViewItem(dr[0].ToString());
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                  lvi.SubItems.Add(dr[i].ToString());
            }
      lstvw1.Items.Add(lvi);
      }
 
Thanks in advance,
Jack. 

Answers (1)