c#sql数据填充到listview失败

ee7vknir  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(283)

我刚接触c#,尝试将mysql数据库中的数据显示到listview,但一直失败。
通过这些编码,我没有得到任何错误,但没有数据显示在列表视图时,我运行程序。
请给我建议和帮助,谢谢

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace sql_to_listview
{
public partial class Form1 : Form
{
    MySqlConnection cn = new MySqlConnection("Data Source=localhost;Initial 
    Catalog=rfiddb;uid=username;pwd=password;");
    MySqlCommand cmd = new MySqlCommand();
    MySqlDataReader dr;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        cn.Open();
        cmd.CommandText = "select * from data";
        cmd.Connection = cn;
        dr = cmd.ExecuteReader();

        while(dr.Read())
        {
            ListViewItem lv = new ListViewItem(dr[0].ToString());
            lv.SubItems.Add(dr[1].ToString());
            lv.SubItems.Add(dr[2].ToString());
            lv.SubItems.Add(dr[3].ToString());
            listView1.Items.Add(lv);
        }
        cn.Close();
    }
  }
}

数据表如下所示

c0vxltue

c0vxltue1#

我相信这就是你想要达到的目标。

如果是,则需要设置 ListViewView 属性到 Details 并添加 ColumnsListView .
以下是您应该使用的代码:

listView1.Columns.Clear(); // Clear previously added columns
    listView1.Items.Clear(); // Clear previously populated items
    listView1.View = View.Details; // Set View property

    // Set Columns
    listView1.Columns.Add("Id"); 
    listView1.Columns.Add("Name");
    listView1.Columns.Add("Number");
    listView1.Columns.Add("Date");

    while(dr.Read())
    {
        ListViewItem lv = new ListViewItem(dr[0].ToString());
        lv.SubItems.Add(dr[1].ToString());
        lv.SubItems.Add(dr[2].ToString());
        lv.SubItems.Add(dr[3].ToString());
        listView1.Items.Add(lv);
    }

查看代码后更新
用这个方法替换窗体加载方法。一旦您了解了要做什么,就用mysql代码替换示例数据。

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    //cn.Open();  
    //cmd.CommandText = "select * from data";
    //cmd.Connection = cn;
    //dr = cmd.ExecuteReader();

    //listView1.Columns.Clear(); // Clear previously added columns
    //listView1.Items.Clear(); // Clear previously populated items
    //listView1.View = View.Details; // Set View property

    //// Set Columns
    //listView1.Columns.Add("Id");
    //listView1.Columns.Add("Name");           
    //listView1.Columns.Add("Number");
    //listView1.Columns.Add("Date");

    //while (dr.Read())
    //{
    //    ListViewItem lv = new ListViewItem(dr[0].ToString());
    //    lv.SubItems.Add(dr[1].ToString());
    //    lv.SubItems.Add(dr[2].ToString());
    //    lv.SubItems.Add(dr[3].ToString());
    //    listView1.Items.Add(lv);
    //}
    //cn.Close();

    List<List<string>> data = new List<List<string>>();
    var row = new List<string>();
    row.Add("1");
    row.Add("Name");
    row.Add("111");
    row.Add(DateTime.Now.AddDays(-2).ToString());
    data.Add(row);

    row = new List<string>();
    row.Add("2");
    row.Add("Name");
    row.Add("222");
    row.Add(DateTime.Now.AddDays(-2).AddMinutes(-5).ToString());
    data.Add(row);

    row = new List<string>();
    row.Add("3");
    row.Add("Name");
    row.Add("333");
    row.Add(DateTime.Now.AddDays(-2).AddMinutes(-10).ToString());
    data.Add(row);

    //lv = new ListView();
    listView1.Columns.Clear();
    listView1.Items.Clear();
    listView1.View = View.Details;
    listView1.Columns.Add("Id");
    listView1.Columns.Add("Name2");
    listView1.Columns.Add("Number");
    listView1.Columns.Add("Date");
    foreach (var dr in data)
    {
        ListViewItem lv = new ListViewItem(dr[0].ToString());
        lv.SubItems.Add(dr[1].ToString());
        lv.SubItems.Add(dr[2].ToString());
        lv.SubItems.Add(dr[3].ToString());
        listView1.Items.Add(lv);
    }
}
eiee3dmh

eiee3dmh2#

你以前有没有试过这种东西?
https://stackoverflow.com/a/4018147/5209563

using(SqlDataReader rdr = cmd.ExecuteReader())
{
    while (rdr.Read())
    {
        var myString = rdr.GetString(0); //The 0 stands for "the 0'th column", so the first column of the result.
        // Do somthing with this rows string, for example to put them in to a list
        listDeclaredElsewhere.Add(myString);
    }
}

相关问题