C# C如何加载listview

C# C如何加载listview,c#,C#,我正在做一个windows窗体应用程序来打开和加载数据库。代码需要显示记录的单记录视图和listView视图,但是我被listView部分卡住了 下面是下面的代码 using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using Sy

我正在做一个windows窗体应用程序来打开和加载数据库。代码需要显示记录的单记录视图和listView视图,但是我被listView部分卡住了

下面是下面的代码

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

namespace Ex3
{
    public partial class changeButton : Form
    {
        public List<Client> ClientRecords = new List<Client>(); //Creates a list for the records to be stored
        public string filename;
        public string fileDir;
        public string rec;
        public bool checkBC;
        public bool checkLC;
        public string dataBaseFile;
        public OleDbConnection conn;

        public changeButton()
        {
            InitializeComponent();
        }

        int position = 0;

        private void Form1_Load(object sender, EventArgs e)
        {
            credBalBox.Maximum = 9999999999;
            savingBalBox.Maximum = 9999999999;
        }

        private void ConnectToDataBase()
        {
            try
            {
                string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                dataBaseFile + ";";
                conn = new OleDbConnection(strConn);
                conn.Open();
            }
            catch (OleDbException e)
            {
                MessageBox.Show("Failed to connect to database: " + e.Message);
            }
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (position < ClientRecords.Count - 1)
            {
                position++;
                Print();
            }
            else
            {
                MessageBox.Show("Outside of the record limit");
            }
        }

        private void previousButton_Click(object sender, EventArgs e)
        {
            if (position > 0)
            {
                position--;
                Print();
            }
            else
            {
                MessageBox.Show("Outside of the record limit");
            }
        }

        public void Print()
        {
            int positionAdd1;
            positionAdd1 = position;
            positionAdd1++;

            currentRecLab.Text = positionAdd1.ToString();
            MaxRecLab.Text = ClientRecords.Count.ToString();

            if (position >= 0 && position < ClientRecords.Count)
            {
                nameBox.Text = ClientRecords[position].Name;
                suburbBox.Text = ClientRecords[position].Suburb;
                postBox.Text = ClientRecords[position].Postcode;
                credBalBox.Text = ClientRecords[position].CreditBal.ToString();
                savingBalBox.Text = ClientRecords[position].SavingBal.ToString();
            }
            else
            {
                nameBox.Text = "";
                suburbBox.Text = "";
                postBox.Text = "";
                credBalBox.Text = "";
                savingBalBox.Text = "";
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            string sameAddName = "null";
            string addName;
            string addSuburb;
            string addPostcode;
            decimal addCredBal;
            decimal addSavBal;

            addName = nameBox.Text;
            addSuburb = suburbBox.Text;
            addPostcode = postBox.Text;
            addCredBal = decimal.Parse(credBalBox.Text);
            addSavBal = decimal.Parse(savingBalBox.Text);

            string sqlPrefix = "INSERT INTO Client VALUES ";
            string data = "(" + "'" + addName + "','" + addSuburb + "','" + addPostcode + "','" + addCredBal + "','" + addSavBal + "'" + ")";
            string sql = sqlPrefix + data;

            foreach (Client client in ClientRecords)
            {
                if (addName == client.Name)
                {
                    sameAddName = "Name is not unique. Please use a unique name";
                }
            }

            if (sameAddName == "null" && addSuburb != null)
            {
                ClientRecords.Add(new Client(addName, addSuburb, addPostcode, addCredBal, addSavBal));

                OleDbCommand cmd = new OleDbCommand(sql, conn);
                cmd.ExecuteNonQuery();

                int positions = ClientRecords.Count;

                currentRecLab.Text = positions.ToString();
                MaxRecLab.Text = positions.ToString();
                nameBox.Text = addName;
                suburbBox.Text = addSuburb;
                postBox.Text = addPostcode;
                credBalBox.Text = addCredBal.ToString();
                savingBalBox.Text = addSavBal.ToString();

                position = ClientRecords.Count - 1;
            }
            else
            {
                if ((sameAddName != "null") && (addSuburb == "") && (addPostcode == ""))
                {
                    MessageBox.Show(sameAddName + "\nAlso, one of the fields are empty. Please fill it in before adding a new record");
                }
                else if (sameAddName != "null")
                {
                    MessageBox.Show(sameAddName);
                }
            }
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            int recPosition = int.Parse(currentRecLab.Text);
            recPosition = recPosition - 1;
            position = 0;

            ClientRecords.Remove(ClientRecords[recPosition]);
            Print();

            if (MaxRecLab.Text == "0")
            {
                currentRecLab.Text = "0";
            }
        }

        private void loadButton_Click_1(object sender, EventArgs e)
        {
            OleDbCommand cmd = new OleDbCommand ("SELECT * FROM Client", conn);

            using (OleDbDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    Client nClient = new Client(rdr[0].ToString(), rdr[1].ToString(), rdr[2].ToString(), decimal.Parse(rdr[3].ToString()), decimal.Parse(rdr[4].ToString()));
                    ClientRecords.Add(nClient);
                    Print();
                }
            }
        }

        private void connectButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "Select DataBase";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                dataBaseFile = dialog.FileName;
                DatabaseNameBox.Text = dataBaseFile;
                ConnectToDataBase();
            }
        }

        private void DatabaseNameBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void nameBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }

    public class Client
    {
        protected string name;
        protected string suburb;
        protected string postcode;
        protected decimal creditBal;
        protected decimal savingBal;

        public Client(string name, string suburb, string postcode, decimal creditBal, decimal savingBal)
        {
            this.name = name;
            this.suburb = suburb;
            this.postcode = postcode;
            this.creditBal = creditBal;
            this.savingBal = savingBal;
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Suburb
        {
            get
            {
                return suburb;
            }
            set
            {
                suburb = value;
            }
        }

        public string Postcode
        {
            get
            {
                return postcode;
            }
            set
            {
                postcode = value;
            }
        }

        public decimal CreditBal
        {
            get
            {
                return creditBal;
            }
            set
            {
                creditBal = value;
            }
        }

        public decimal SavingBal
        {
            get
            {
                return savingBal;
            }
            set
            {
                savingBal = value;
            }
        }
    }
}

我希望能够将代码放在这里,当我从上方单击加载按钮时,它应该加载listview

要加载listview,我将使用以下函数-它使用字符串数组,listviewitem会向listview添加项目

private void LoadListview()
{
    string NAME = "John DOE";
    string AGE = "30";
    string SEX = "MALE";
    string DOB = "08/28/1988";

    string[] rowa = { NAME, AGE, SEX, DOB };
    var listViewItema = new ListViewItem(rowa);

    listView1.Items.Add(listViewItema);
    listView1.Items.Add(listViewItema);
    listView1.Items.Add(listViewItema);
    listView1.Items.Add(listViewItema);

}

你可以在我的网站上找到一个分步详细的答案:

你到底坚持什么?ListView具有可以用ListViewItems填充的.Items属性。您确定不想改为使用DataGrid吗?我希望能够在loadButton\u Click\u 1事件中显示完整的数据库视图,而不是打印命令?ListView是一个经常被滥用的控件。它不是网格控件。除非需要多个视图或分组,否则不应使用ListView。使用更简单、更恰当的方法,即填充一个列表(可能是DataTable或其他内容),并将其绑定到DataGridView。
private void LoadListview()
{
    string NAME = "John DOE";
    string AGE = "30";
    string SEX = "MALE";
    string DOB = "08/28/1988";

    string[] rowa = { NAME, AGE, SEX, DOB };
    var listViewItema = new ListViewItem(rowa);

    listView1.Items.Add(listViewItema);
    listView1.Items.Add(listViewItema);
    listView1.Items.Add(listViewItema);
    listView1.Items.Add(listViewItema);

}