Vuk Stanojevic

Vuk Stanojevic

  • 1.5k
  • 140
  • 41.9k

C# listview header name=value from database

Apr 11 2020 11:16 AM
Hi Everyone,
 
I'm trying to create listview where column header name is retrieved from database (its number)
 
When i execute my code i get as column name System.Data.DataRow but it should be number from database.
 
My code is:
  1. SqlConnection con = new SqlConnection(@"Data Source=MAIN-PC\SQLEXPRESS;Integrated Security=True");  
  2. SqlCommand cmd = new SqlCommand("USE "+comboBox1.Text+" ; Select StoreCode FROM stores",con);  
  3. DataTable dt = new DataTable();  
  4. con.Open();  
  5. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  6. da.Fill(dt);  
  7. ListView view = new ListView();  
  8. view.View = View.Details;  
  9. listView1.Columns.Add("Store", 90, HorizontalAlignment.Left);  
  10. foreach(DataRow row in dt.Rows)  
  11. {  
  12. listView1.Columns.Add(row.ToString(), 90, HorizontalAlignment.Left);  
  13. } 
Result is: 

Answers (2)