Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

How to bind a listview to a database?

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

Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

Try:

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

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

ASKER

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
Could you please translate it in reall code?

Peter
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.
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
ASKER CERTIFIED SOLUTION
Avatar of David L. Hansen
David L. Hansen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have just adapter and a dataset:
            TableAdapter1
            dbRootDataSet1
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