How to bind data in listview?

I usually use

gridview.Datasource=ds.Tables[0];

for my datagridview. How can I do this in listview?

Dani AI

Generated

Short clarification and why the thread looked inconsistent: there are multiple distinct "ListView" controls. The ASP.NET server ListView supports setting a DataSource and declarative/data-bound templates (the web ListView approach shown in and 's links). The Windows Forms ListView you opened in Visual C# is a different control (System.Windows.Forms.ListView) and is not data-bound the same way — you populate its Items and Columns yourself. (ASP.NET ListView documentation, WinForms ListView class).

Practical tips for the WinForms ListView (the one ended up using): do not add column headers on every click — either add them once (design time or guarded code) or clear the headers before re-adding. Clearing only the items differs from clearing both items and headers: the control exposes a method that removes items only and a separate method that clears items plus columns (note: groups are not removed by that method). Use the appropriate clear operation to avoid duplicated columns. (Items.Clear behaviour, ListView.Clear behaviour).

Performance and robustness tips: wrap bulk add/remove operations with the control's BeginUpdate/EndUpdate to avoid flicker and speed up inserts, and consider VirtualMode when the data set is very large so items are created on demand. If true data-binding is required, choose a control that supports DataSource (for example DataGridView) or use a third-party bindable ListView implementation. (BeginUpdate/EndUpdate, VirtualMode).

Recommended Answers

All 8 Replies

Listview1.Datasource=ds

ds must be your datasouce or datatable

Listview1.Datasource=ds

ds must be your datasouce or datatable

Interesting because when I pop open my trusty visual C# in VS and create a listView... it doesn't have a Datasource property.

Which, was also indicated in the link that pritesh supplied :) By the way, good link pritesh, perhaps a bit confusing for a beginner but the theory is all there.

Edit: Now THAT link, hirenpatel, is definitely going to do the trick as well :twisted:

I've used this

{
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student", con);
            da.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];

            listView1.Columns.Add("Student_No", 10, HorizontalAlignment.Left);

            foreach(DataRow dr in dt.Rows)
            {
                ListViewItem lvi = new ListViewItem(dr["Student_No"].ToString());
                lvi.Tag = dr;
                listView1.Items.Add(lvi);
            }
        }

But the listview is all mess up. The 270070032 student_no, goes something like '2700...', and it's mess up. How to fix this?

I've fixed it!

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student", con);
            da.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];

            listView1.View = View.Details;
            listView1.GridLines = true;

            listView1.Columns.Add("Student_No", 150, HorizontalAlignment.Left);

            foreach(DataRow dr in dt.Rows)
            {
                ListViewItem lvi = new ListViewItem(dr["Student_No"].ToString());
                lvi.Tag = dr;
                listView1.Items.Add(lvi);
            }

Whenever I click the button, it doesn't clear the listview even though I have used this

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Student", con);
            da.Fill(ds);
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
[B]listView1.Items.Clear();[/B]

EDIT: The items are now being cleared by clearing the dataset also.
But the columns are being duplicated for every click. How to fix this?!

It works now! Instead of .Items, I remove it and made it listView1.Clear() only.

Populate and clearing listview solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.