353 lines
12 KiB
C#
353 lines
12 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using DevExpress.XtraGrid.Views.Grid;
|
|
using DevExpress.XtraGrid.Columns;
|
|
using System.IO;
|
|
using System.Xml;
|
|
using System.Xml.Serialization;
|
|
using DevExpress.Utils;
|
|
using WMS.Common;
|
|
|
|
namespace WMS.Ctrl
|
|
{
|
|
public partial class FrmGridViewColSelect : Form
|
|
{
|
|
#region 定义变量
|
|
private GridView _MyGridView = null;
|
|
private string _MyConfigName = "";
|
|
private DataTable _MyGroupDTable = null;
|
|
#endregion
|
|
|
|
#region 定义属性
|
|
/// <summary>
|
|
/// 汇总数据
|
|
/// </summary>
|
|
public DataTable MyGroupDTable
|
|
{
|
|
get { return _MyGroupDTable; }
|
|
}
|
|
#endregion
|
|
|
|
#region 构造函数
|
|
/// <summary>
|
|
/// 构造函数
|
|
/// </summary>
|
|
/// <param name="gv"></param>
|
|
public FrmGridViewColSelect(GridView gv, string configName)
|
|
{
|
|
InitializeComponent();
|
|
_MyGridView = gv;
|
|
_MyConfigName = configName;
|
|
}
|
|
#endregion
|
|
|
|
#region 初始化
|
|
/// <summary>
|
|
/// 初始化
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void FrmGridViewColSelect_Load(object sender, EventArgs e)
|
|
{
|
|
try
|
|
{
|
|
this.Cursor = Cursors.WaitCursor;
|
|
//获取列信息
|
|
ColumnsList myColumnsList = new ColumnsList();
|
|
List<ColumnsList> myColList = myColumnsList.LoadQueryColumn(_MyGridView);
|
|
|
|
//加载汇总列选项
|
|
LoadGroutColConfig(myColList);
|
|
|
|
//绑定
|
|
this.gc_GroupList.DataSource = myColList;
|
|
|
|
//选项
|
|
if (this._MyGridView.GetSelectedRows().Length > 1)
|
|
{
|
|
this.chkGroupSelectRow.Checked = true;
|
|
this.chkGroupAll.Checked = false;
|
|
}
|
|
else
|
|
{
|
|
this.chkGroupSelectRow.Checked = false;
|
|
this.chkGroupAll.Checked = true;
|
|
}
|
|
|
|
this.Cursor = Cursors.Default;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.Cursor = Cursors.Default;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 加载汇总列配置
|
|
/// <summary>
|
|
/// 加载汇总列配置
|
|
/// </summary>
|
|
private void LoadGroutColConfig(List<ColumnsList> myColList)
|
|
{
|
|
try
|
|
{
|
|
//判断是否有配置文件
|
|
string filePaht = Application.StartupPath + @"\Group" + @"\" + _MyConfigName + ".xml";
|
|
if (File.Exists(filePaht) == false) return;
|
|
|
|
//读取配置文件到LIST中
|
|
FileStream fs = null;
|
|
XmlSerializer xs = new XmlSerializer(typeof(List<ColumnsList>));
|
|
fs = new FileStream(filePaht, FileMode.Open, System.IO.FileAccess.Read);
|
|
List<ColumnsList> tlist = null;
|
|
tlist = (List<ColumnsList>)xs.Deserialize(fs);
|
|
fs.Close();
|
|
|
|
//校验
|
|
if (tlist == null) return;
|
|
if (tlist.Count <= 0) return;
|
|
|
|
//替换值
|
|
foreach (ColumnsList item in tlist)
|
|
{
|
|
List<ColumnsList> flist = myColList.FindAll(delegate(ColumnsList p)
|
|
{ return p.FieldName == item.FieldName && p.ColumnType == item.ColumnType; });
|
|
if (flist.Count > 0)
|
|
{
|
|
flist[0].Check = item.Check;
|
|
if (item.Check == true && item.IsSum == false)
|
|
{
|
|
flist[0].IsSum = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
#endregion
|
|
|
|
#region 保存汇总列配置
|
|
/// <summary>
|
|
/// 保存汇总列配置
|
|
/// </summary>
|
|
private void SaveGroutColConfig(List<ColumnsList> myColList)
|
|
{
|
|
try
|
|
{
|
|
//创建文件夹
|
|
string filePaht = Application.StartupPath + @"\Group";
|
|
if (Directory.Exists(filePaht) == false) Directory.CreateDirectory(filePaht);
|
|
|
|
//创建文件路径
|
|
filePaht = filePaht + @"\" + _MyConfigName + ".xml";
|
|
if (File.Exists(filePaht) == true) File.Delete(filePaht);
|
|
|
|
//保存XML
|
|
FileStream fs = null;
|
|
XmlSerializer xs = new XmlSerializer(typeof(List<ColumnsList>));
|
|
fs = new FileStream(filePaht, FileMode.Create, System.IO.FileAccess.Write);
|
|
xs.Serialize(fs, myColList);
|
|
fs.Close();
|
|
}
|
|
catch (Exception)
|
|
{ }
|
|
}
|
|
#endregion
|
|
|
|
#region 添加行信息
|
|
/// <summary>
|
|
/// 添加行信息
|
|
/// </summary>
|
|
private void AddRowInfo(int row, List<ColumnsList> myColList)
|
|
{
|
|
//分组行
|
|
if (this._MyGridView.GetRow(row) == null) return;
|
|
|
|
//创建行数据
|
|
string keyWhere = " 1=1 ";
|
|
DataRow rowInfo = _MyGroupDTable.NewRow();
|
|
foreach (ColumnsList item in myColList)
|
|
{
|
|
object value = this._MyGridView.GetRowCellDisplayText(row, item.FieldName);
|
|
if (value == null || value == "") value = null;
|
|
|
|
string fieldName = item.Caption;
|
|
if (item.IsSum == true) fieldName = "*" + item.Caption;
|
|
|
|
if (value != null) rowInfo[fieldName] = value;
|
|
|
|
if (item.IsSum == false)
|
|
{
|
|
if (value == null)
|
|
{
|
|
keyWhere = keyWhere + " And " + fieldName + " is null";
|
|
}
|
|
else
|
|
{
|
|
if (item.ColumnType == "string")
|
|
{
|
|
keyWhere = keyWhere + " And " + fieldName + "='" + value.ToString() + "'";
|
|
}
|
|
else
|
|
{
|
|
keyWhere = keyWhere + " And " + fieldName + "=" + value.ToString() + "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//判断行是否存在
|
|
DataRow[] rs = _MyGroupDTable.Select(keyWhere);
|
|
if (rs.Length > 0)
|
|
{
|
|
foreach (ColumnsList item in myColList)
|
|
{
|
|
if (item.IsSum == true)
|
|
{
|
|
string fieldName = "*" + item.Caption;
|
|
|
|
decimal cValue = 0;
|
|
decimal fValue = 0;
|
|
|
|
object fieldValue = rs[0][fieldName];
|
|
if (SystemCommon.IsPositiveInteger(fieldValue.ToString()) == true)
|
|
{
|
|
if (fieldValue != null) cValue = decimal.Parse(fieldValue.ToString());
|
|
}
|
|
|
|
fieldValue = rowInfo[fieldName];
|
|
if (SystemCommon.IsPositiveInteger(fieldValue.ToString()) == true)
|
|
{
|
|
if (fieldValue != null) fValue = decimal.Parse(fieldValue.ToString());
|
|
}
|
|
rs[0][fieldName] = cValue + fValue;
|
|
|
|
}
|
|
}
|
|
rs[0]["#统计行数"] = decimal.Parse(rs[0]["#统计行数"].ToString()) + 1;
|
|
}
|
|
else
|
|
{
|
|
rowInfo["#统计行数"] = 1;
|
|
_MyGroupDTable.Rows.Add(rowInfo);
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region 按钮事件
|
|
/// <summary>
|
|
/// 确定
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void bOK_Click(object sender, EventArgs e)
|
|
{
|
|
WaitDialogForm wdf = null;
|
|
try
|
|
{
|
|
//创建汇总列
|
|
this.gv_GroupList.CloseEditor();
|
|
this.gv_GroupList.UpdateCurrentRow();
|
|
List<ColumnsList> myColList = new List<ColumnsList>();
|
|
_MyGroupDTable = new DataTable();
|
|
for (int row = 0; row < this.gv_GroupList.RowCount; row++)
|
|
{
|
|
ColumnsList item = this.gv_GroupList.GetRow(row) as ColumnsList;
|
|
if (item == null) continue;
|
|
if (item.Check == false) continue;
|
|
if (item.IsSum == true && item.ColumnType == "string")
|
|
{
|
|
MessageBox.Show("不可以针对字符型字段进行汇总", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
return;
|
|
}
|
|
string fieldName = item.Caption;
|
|
if (item.IsSum == true)
|
|
{
|
|
fieldName = "*" + item.Caption;
|
|
}
|
|
if (item.ColumnType == "string")
|
|
{
|
|
_MyGroupDTable.Columns.Add(fieldName, typeof(string));
|
|
}
|
|
else
|
|
{
|
|
_MyGroupDTable.Columns.Add(fieldName, typeof(Decimal));
|
|
}
|
|
myColList.Add(item);
|
|
}
|
|
if (myColList.Count <= 0)
|
|
{
|
|
MessageBox.Show("请选择汇总列", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
return;
|
|
}
|
|
|
|
//统计汇总行数
|
|
_MyGroupDTable.Columns.Add("#统计行数", typeof(int));
|
|
|
|
this.Cursor = Cursors.WaitCursor;
|
|
wdf = new WaitDialogForm("......", "正在汇总数据,请等待");
|
|
|
|
//保存汇总列配置
|
|
SaveGroutColConfig(myColList);
|
|
|
|
//创建汇总数据
|
|
if (this.chkGroupAll.Checked == true)
|
|
{
|
|
for (int row = 0; row < this._MyGridView.RowCount; row++)
|
|
{
|
|
AddRowInfo(row, myColList);
|
|
}
|
|
}
|
|
|
|
if (this.chkGroupSelectRow.Checked == true)
|
|
{
|
|
int[] rows = this._MyGridView.GetSelectedRows();
|
|
for (int i = 0; i < rows.Length; i++)
|
|
{
|
|
AddRowInfo(rows[i], myColList);
|
|
}
|
|
}
|
|
|
|
if (_MyGroupDTable.Rows.Count <= 0)
|
|
{
|
|
if (wdf != null) wdf.Close();
|
|
this.Cursor = Cursors.Default;
|
|
MessageBox.Show("无汇总数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
return;
|
|
}
|
|
|
|
//返回
|
|
this.DialogResult = DialogResult.OK;
|
|
this.Close();
|
|
if (wdf != null) wdf.Close();
|
|
this.Cursor = Cursors.Default;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (wdf != null) wdf.Close();
|
|
this.Cursor = Cursors.Default;
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关闭
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
private void bCancel_Click(object sender, EventArgs e)
|
|
{
|
|
this.DialogResult = DialogResult.Cancel;
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|