如題,最近成功將資料放進listview裡,但不太懂怎麼放進這樣的listview裡
外觀如下圖
請問該如何做才能放入左邊的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)
{
}
}
}
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)";
}
}
}
}
}
}