Wahtever
Wahtever

Reputation: 3678

How to populate a listview from a datatable

I have a ListView and a DataTable and I would like to know how to populate the ListView from the DataTable.

Here is my code:

DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(@"connectionstring");
SqlCommand ccmd = new SqlCommand("SELECT img FROM '" + user + "'", conn);
conn.Open();

SqlDataAdapter da = new SqlDataAdapter(ccmd);
da.SelectCommand = ccmd;
da.Fill(dt);

Upvotes: 4

Views: 5965

Answers (1)

Mo Valipour
Mo Valipour

Reputation: 13486

Assuming that you have already designed your ListView (and bindings inside it) in your markup here is the code you need in your server side to load data into listView:

listView.DataSource = dt;
listView.DataBind();

Upvotes: 5

Related Questions