We help IT Professionals succeed at work.

How to bind a listview to a database?

Peter Kiers
Peter Kiers used Ask the Experts™
on
Hi,

How can I bind a listview to a database?

My preferences are with less code I have used the datasource-wizard
to connect to the database. Now somehow i have to hoop up the listview
to the datasource. But i don't know how?

Who can help me?
Peter Kiers



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.notesTableAdapter1.Fill(this.dbRootDataSet1.Notes);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            notesTableAdapter1.Update(dbRootDataSet1);
            dbRootDataSet1.AcceptChanges();
        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try:

myListView.DataSource = this.dbRootDataSet1["Notes"];

ps. You can set any datatable equal to almost any control that has a "datasource" property.
Peter KiersOperator

Author

Commented:
Or i am blind but listview does't have a property DataSource !!!
Sorry, forgot about that (I go looking for it everytime) *shrug*

So, what I end up doing is just looping through the one column in my datatable and enter them one at a time in the text property of the listview...a pain, I know.  Another approach is not to use a listview at all, but a datagridview instead (showing only one column).  That way you get the ease of using the "datasource" property, but it is a bit of an overkill.
Sorry again, I guess I'm still a little drowsey, you fill the listview's ITEMS property using the ADD method like this:

For Each r As Datarow In myDataTable.Rows
   me.ListView1.Items.Add(r("myColumnName").ToString)
Next
Peter KiersOperator

Author

Commented:
Could you please translate it in reall code?

Peter
Peter KiersOperator

Author

Commented:
And where exactly to put it?
foreach (DataRow r in myDataTable.Rows) {
      this.ListView1.Items.Add(r["myColumnName"].ToString());
}

As soon as you have a filled datatable (ie. You've run your query) you can use this code and it will fill the ListView.
Peter KiersOperator

Author

Commented:
I have this:

        private void MainForm_Load(object sender, EventArgs e)
        {
            this.notesTableAdapter1.Fill(this.dbRootDataSet1.Notes);
            foreach (DataRow r in myDataTable.Rows)
            {
                this.listView1.Items.Add(r["Caption"].ToString());
            }

        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            notesTableAdapter1.Update(dbRootDataSet1);
            dbRootDataSet1.AcceptChanges();
        }

And a message:

The name 'myDataTable' does not exist in the current context

Peter
What is the name of the table in the database that you want to use to fill the listview with?
Peter KiersOperator

Author

Commented:
I have just adapter and a dataset:
            TableAdapter1
            dbRootDataSet1
Peter KiersOperator

Author

Commented:
I go it:


   
private void MainForm_Load(object sender, EventArgs e)
        {
            this.notesTableAdapter1.Fill(this.dbRootDataSet1.Notes);
            LoadList();

        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            notesTableAdapter1.Update(dbRootDataSet1);
            dbRootDataSet1.AcceptChanges();
        }


        private void LoadList()
        {
            // Get the table from the data set
            DataTable dtable = dbRootDataSet1.Tables["Notes"];

            // Clear the ListView control
            listView1.Items.Clear();

            // Display items in the ListView control
            for (int i = 0; i < dtable.Rows.Count; i++)
            {
                DataRow drow = dtable.Rows[i];

                // Only row that have not been deleted
                if (drow.RowState != DataRowState.Deleted)
                {
                    // Define the list items
                    ListViewItem lvi = new ListViewItem(drow["Caption"].ToString());
                    lvi.SubItems.Add(drow["Creation"].ToString());
                    lvi.SubItems.Add(drow["Content"].ToString());

                    // Add the list items to the ListView
                    listView1.Items.Add(lvi);
                }
            }
        }

    }

}

Open in new window