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.
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Dipankar BiswasPosted May 5, 2015, 3:15 PM
see this links...to help
https://social.msdn.microsoft.com/Forums/en-US/eac31f73-58c0-4149-8d9b-ceabc805f001/populate-a-listview-from-datatable?forum=vbgeneral
http://stackoverflow.com/questions/18584707/how-to-add-items-in-a-datatable-to-a-listview