iT邦幫忙

0

請問SQL裡的資料如何放進listview裡分類並顯示?

  • 分享至 

  • xImage

如題,最近成功將資料放進listview裡,但不太懂怎麼放進這樣的listview裡
外觀如下圖
https://ithelp.ithome.com.tw/upload/images/20180605/20110132eeoeRrwYVI.jpg

請問該如何做才能放入左邊的listview裡,且上面group行為裡的字還能幫我計算該
行為裡有幾個ITEM,希望能為我講解一下,謝謝。

這是我目前的程式碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.Properties;

namespace WindowsFormsApp1.HW
{
    public partial class listview : Form
    {
        public listview()
        {
            InitializeComponent();
            LoadCountryToComboBox();
            CreateListViewColumns();
        }

        private void CreateListViewColumns()
        {
            this.listView1.View = View.Details;

            //select
            try
            {
                using (SqlConnection conn = new SqlConnection(Settings.Default.MyNWconnectionString))
                {
                    using (SqlCommand command = new SqlCommand("select * from customers", conn))
                    {
                        conn.Open();
                        using (SqlDataReader dataReader = command.ExecuteReader())
                        {
                            DataTable table1 = dataReader.GetSchemaTable();
                            this.dataGridView1.DataSource = table1;

                            for (int i = 0; i <= table1.Rows.Count - 1; i++)
                            {
                                this.listView1.Columns.Add(table1.Rows[i][0].ToString());
                            }
                            this.listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
                        }
                    } //auto command.Dispose()
                } //auto  conn.close()=>conn.Dispose()
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadCountryToComboBox()
        {
            //select
            try
            {
                using (SqlConnection conn = new SqlConnection(Settings.Default.MyNWconnectionString))
                {
                    using (SqlCommand command = new SqlCommand("select distinct country from customers", conn))
                    {
                        conn.Open();
                        using (SqlDataReader dataReader = command.ExecuteReader())
                        {
                            this.comboBox1.Items.Clear();
                            while (dataReader.Read())
                            {
                                this.comboBox1.Items.Add(dataReader["Country"]);
                            }
                            this.comboBox1.SelectedIndex = 0;
                        }
                    } //auto command.Dispose()
                } //auto  conn.close()=>conn.Dispose()
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void largeIconToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.LargeIcon;
        }

        private void smallIconToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.SmallIcon;
        }

        private void detailToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.Details;
        }
        //TODO  ListView.....
        // 1. All Country
        // 2. group by (listview - groups) USA (100)
        // 3. order by     

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            try
            {

                using (SqlConnection conn = new SqlConnection(Settings.Default.MyNWconnectionString))
                {
                    using (SqlCommand command = new SqlCommand("select *from customers where Country ='" + comboBox1.Text + "' ", conn))
                    {
                        conn.Open();
                        using (SqlDataReader dataReader = command.ExecuteReader())
                        {

                            this.listView1.Items.Clear();
                            Random r = new Random();
                            while (dataReader.Read())
                            {
                                ListViewItem x = listView1.Items.Add(dataReader[0].ToString());
                                x.ImageIndex = r.Next(0, ImageList1.Images.Count);
                                if (x.Index % 2 == 0)
                                {
                                    x.BackColor = Color.Red;
                                    x.ForeColor = Color.Gray;
                                }
                                else
                                {
                                    x.BackColor = Color.White;
                                    x.ForeColor = Color.Black;
                                }
                                for (int i = 1; i <= dataReader.FieldCount - 1; i++)
                                {
                                    if (dataReader.IsDBNull(i))
                                    {
                                        x.SubItems.Add("空值");
                                    }
                                    else
                                    {
                                        x.SubItems.Add(dataReader[i].ToString());
                                    }

                                }
                            }
                         
                        }
                    } //auto command.Dispose()
                } //auto  conn.close()=>conn.Dispose()
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void listView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

小魚 iT邦大師 1 級 ‧ 2018-06-05 20:14:02 檢舉
那看起來像是combobox ,
你要的是combobox還是listview ?
tenno081 iT邦研究生 4 級 ‧ 2018-06-05 21:33:58 檢舉
你好,是combobox選出來的東西呈現在listview上,例如我目前combobox選到的國家然後下面那個listview裡面呈現我所選到的選項裡的內容,我希望他能以上面那個
listview的格式那樣顯示出來
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
JamesDoge
iT邦高手 1 級 ‧ 2023-02-10 08:28:40
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace YourProjectName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadDataFromSQLToListView();
        }

        private void LoadDataFromSQLToListView()
        {
            //資料庫連線字串
            string connectionString = "Data Source=YourServerName;Initial Catalog=YourDBName;Integrated Security=True";

            //SQL語法
            string sql = "SELECT column1, column2, column3, column4 FROM YourTableName";

            //建立資料庫連線
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                //建立SQL命令
                using (SqlCommand command = new SqlCommand(sql, connection))
                {
                    //執行SQL命令並將結果存入DataTable中
                    DataTable dataTable = new DataTable();
                    SqlDataAdapter adapter = new SqlDataAdapter(command);
                    adapter.Fill(dataTable);

                    //清空ListView
                    listView1.Items.Clear();
                    listView1.Groups.Clear();

                    //將DataTable中的資料新增至ListView中
                    foreach (DataRow row in dataTable.Rows)
                    {
                        //取得每筆資料的column1值,作為Group的Header
                        string header = row["column1"].ToString();

                        //判斷Group是否已存在
                        ListViewGroup group = listView1.Groups[header];
                        if (group == null)
                        {
                            //若Group不存在,則新增一個Group
                            group = new ListViewGroup(header);
                            listView1.Groups.Add(group);
                        }

                        //將資料新增至ListView中
                        ListViewItem item = new ListViewItem(header, group);
                        item.SubItems.Add(row["column2"].ToString());
                        item.SubItems.Add(row["column3"].ToString());
                        item.SubItems.Add(row["column4"].ToString());
                        listView1.Items.Add(item);
                    }

                    //統計每個Group中有幾個ITEM
                    foreach (ListViewGroup group in listView1.Groups)
                    {
                        group.Header = group.Header + " (" + group.Items.Count + " items)";
                    }
                }
            }
        }
    }
}

我要發表回答

立即登入回答