Avatar of Peter Kiers
Peter Kiers
Flag 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

.NET ProgrammingC#

Avatar of undefined
Last Comment
Peter Kiers

8/22/2022 - Mon
David L. Hansen

Try:

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

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

ASKER
Or i am blind but listview does't have a property DataSource !!!
David L. Hansen

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
David L. Hansen

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 Kiers

ASKER
Could you please translate it in reall code?

Peter
Peter Kiers

ASKER
And where exactly to put it?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
David L. Hansen

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 Kiers

ASKER
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
David L. Hansen

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Peter Kiers

ASKER
I have just adapter and a dataset:
            TableAdapter1
            dbRootDataSet1
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Peter Kiers

ASKER
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