我编写了一个代码,将SQL中的数据传递给listview。 [here screenshot of the data pulled and shown on listview. ] 1
如何使用headid在此列表视图上创建组。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Timers;
namespace AbhiProject{
public partial class daybook_credit : Form
{
public daybook_credit()
{
InitializeComponent();
BindGrid();
}
//int ID = 0;
private SqlConnection con;
private SqlCommand cmd;
private SqlDataAdapter adapt;
private DataTable dt;
//Connection String
string cs = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Abhilash\Documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\db_all.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
private void BindGrid()
{
listView1.GridLines = true;
listView1.View = View.Details;
//Add Column Header
listView1.Columns.Add("ID", 50);
listView1.Columns.Add("Head ID", 150);
listView1.Columns.Add("Date", 150);
listView1.Columns.Add("Receipt No", 150);
listView1.Columns.Add("Details", 150);
listView1.Columns.Add("Cash Credit", 150);
listView1.Columns.Add("Transfer Credit Adjustment", 150);
con = new SqlConnection(cs);
con.Open();
// Chnage sql query and table name
cmd = new SqlCommand("Select * from daybook_credit ORDER BY headid ASC", con);
SqlDataReader Reader = cmd.ExecuteReader();
listView1.Items.Clear();
while (Reader.Read())
{
listView1.Items.Add(new ListViewItem(new string[] { Reader.GetInt32(0).ToString(), Reader.GetInt32(1).ToString(), Reader.GetString(2), Reader.GetString(3), Reader.GetString(4), Reader.GetInt32(5).ToString(), Reader.GetInt32(6).ToString() }, Reader.GetInt32(1).ToString()));
}
//MessageBox.Show(Reader.GetInt32(1).ToString());
Reader.Close();
con.Close();
}
}}
我如何对这些数据进行分组,我也想知道是否可以找到每个组的现金信用额度
答案 0 :(得分:0)
实际上你不能直接在gridview上进行分组,
相反,请根据您的要求创建自定义 class
,
public class Sample
{
public float TransferCredit { get; set; }
public float CashCredit { get; set; }
public DateTime Date { get; set; }
public int HeadID { get; set; }
public int ID { get; set; }
public int RecieptNo { get; set; }
}
然后使用相同的并填写列表
List<Sample> result = new List<Sample>();
while (Reader.Read())
{
var Sample = new Sample
{
TransferCredit = reader.GetInt32(0),
CashCredit = reader.GetString(1)
......
}
result.Add(foo);
}
然后您可以使用linq
执行GroupBy
result.GroupBy(t => t.HeadID);
结果。