iT邦幫忙

0

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

如題,最近成功將資料放進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邦新手 5 級 ‧ 2018-06-05 21:33:58 檢舉
你好,是combobox選出來的東西呈現在listview上,例如我目前combobox選到的國家然後下面那個listview裡面呈現我所選到的選項裡的內容,我希望他能以上面那個
listview的格式那樣顯示出來

尚未有邦友回答

立即登入回答