C#行列转换

public partial class Frm_DianjianRecord : Frm_Base
{
public Frm_DianjianRecord()
{
InitializeComponent();
}

/// <summary>
/// 列表列信息
/// </summary>
IList<ColClass> collist = new List<ColClass>();

/// <summary>
/// 初期化加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Frm_DianjianRecord_Load(object sender, EventArgs e)
{
#region 页面初始化
//tool工具栏加背景图
toolStrip_btn.BackgroundImage = RFIDMg.Properties.Resources.tooldown;
this.panel_cx.BackgroundImage = null;
//配置时间控件,默认为当天日期
this.dtpWorkDateFrom.Format = DateTimePickerFormat.Custom;
this.dtpWorkDateFrom.Text = DateTime.Now.ToString("yyyy-MM-dd");
this.dtpWorkDateTo.Format = DateTimePickerFormat.Custom;
this.dtpWorkDateTo.Text = DateTime.Now.ToString("yyyy-MM-dd");
//绑定下拉菜单
pubCombox.SetComboBoxByTable<T_Base_LineInfo>(cboLineCode, "T_Base_Line", "lineName", "lineCode", "所有");//绑定线体
pubCombox.SetComboBoxByTable<T_Base_BanciInfo>(cboBanciCode, "T_Base_Banci", "banciName", "banciCode", "所有");//绑定岗位
//初始化gird
//InitGrid();
//加载数据
//GetData();
#endregion
}

/// <summary>
/// 初始化gird
/// </summary>
public void InitGrid()
{
collist.Clear();
this.table_data.BeginUpdate();
#region 创建列标题
this.table_data.ColumnModel.Columns.Clear();
//第一列隐藏是自动增长列,便于修改/删除
pubtable.ColAddNew(table_data, collist, "txt", "key", "fkey", 50, false);
pubtable.ColAddNew(table_data, collist, "txt", "序号", "xuhao", 50, true);
pubtable.ColAddNew(table_data, collist, "txt", "工作日期", "workDate", 90, true);
pubtable.ColAddNew(table_data, collist, "txt", "线体", "lineName", 100, true);
pubtable.ColAddNew(table_data, collist, "txt", "站点", "zhandianName", 100, true);
pubtable.ColAddNew(table_data, collist, "txt", "岗位", "gangweiName", 100, true);
pubtable.ColAddNew(table_data, collist, "txt", "班次", "banciName", 100, true);
pubtable.ColAddNew(table_data, collist, "txt", "班组", "banzuName", 100, true);
pubtable.ColAddNew(table_data, collist, "txt", "点检顺序", "dianjianOrderNo", 70, true);
pubtable.ColAddNew(table_data, collist, "txt", "点检标准", "dianjianStr", 70, true);

//循环加载动态列
IList list = null;
T_TQM_DianjianRecordInfoImp imp = new T_TQM_DianjianRecordInfoImp();
//根据条件查询

string lineCode = this.cboLineCode.SelectedValue.ToString();
string zhandianCode = this.lblZhandianCode.Text;
string gangweiCode = this.lblGangweiCode.Text;
string banciCode = this.cboBanciCode.SelectedValue.ToString();

string workDateFrom = this.dtpWorkDateFrom.Text;
string workDateTo = this.dtpWorkDateTo.Text;

if (lineCode == "所有")
{
lineCode = "";
}
if (banciCode == "所有")
{
banciCode = "";
}
/*重点看*/

list = (IList)imp.GetListBysearchHour(lineCode, zhandianCode, gangweiCode, banciCode, workDateFrom, workDateTo);
foreach (T_TQM_DianjianRecordInfo info in list)
{
string fieldHour = "FABC"+info.DjTime;
pubtable.ColAddNew(table_data, collist, "txt", info.DjTime, fieldHour, 50, true);
pubtable.ColAddNew(table_data, collist, "txt", "点检时间", fieldHour + "Time", 140, true);
pubtable.ColAddNew(table_data, collist, "txt", "点检人", fieldHour + "User", 60, true);
}
#endregion

//设置属性
pubtable.TableInit(this.table_data);
this.table_data.EndUpdate();
}

/// <summary>
/// 加载数据
/// </summary>
public void GetData()
{
#region 根据查询条件获取数据

InitGrid();

IList list = null;
T_TQM_DianjianRecordInfoImp imp = new T_TQM_DianjianRecordInfoImp();
//根据条件查询

string lineCode = this.cboLineCode.SelectedValue.ToString();
string zhandianCode = this.lblZhandianCode.Text;
string gangweiCode = this.lblGangweiCode.Text;
string banciCode = this.cboBanciCode.SelectedValue.ToString();

string workDateFrom = this.dtpWorkDateFrom.Text;
string workDateTo = this.dtpWorkDateTo.Text;

if (lineCode == "所有")
{
lineCode = "";
}
if (banciCode == "所有")
{
banciCode = "";
}
list = (IList)imp.GetListBysearch(lineCode,zhandianCode,gangweiCode,banciCode,workDateFrom, workDateTo);
//数据加载到xptable中
LoadData(list);
//行数
toolStripStatusLabelcount.Text = " 记录数:" + list.Count.ToString();
this.table_data.TableModel.Selections.Clear();
#endregion
}

/// <summary>
/// 数据加载到xptable中
/// </summary>
/// <param name="list">数据链表</param>
public void LoadData(IList list)//重点看怎么加载数据
{
this.table_data.BeginUpdate();
this.table_data.TableModel.Rows.Clear();
int i = 1;
//定义特殊单元格的样式
CellStyle cellStyle = new CellStyle();
cellStyle.ForeColor = Color.Red;
cellStyle.Font = new Font("宋体", 9, FontStyle.Bold);
string fkey = "";

//循环数据集进行加载数据
foreach (T_TQM_DianjianRecordInfo info in list)
{
if (fkey != info.fkey)
{
//新增一行
fkey = info.fkey;

#region 动态加载每列数据
Type type = info.GetType();
Row row = new Row();
foreach (ColClass colinfo in collist)
{
Cell cellnew = new Cell();
if (colinfo.fcolstyle == "btn")
{
//按钮直接取ffield变量 不是从数据库读取
cellnew.Text = colinfo.ffield;
cellnew.ForeColor = ColorTranslator.FromHtml("#0052b3");
}
else
{
if (colinfo.ffield == "xuhao")
{
//序号为前台生成的
cellnew.Text = i.ToString();
cellnew.ForeColor = ColorTranslator.FromHtml("#0052b3");
}
else if (colinfo.ffield == "check")
{
//选择框前台 默认 否
cellnew.Checked = false;
cellnew.ForeColor = Color.Red;
}
else
{
//数据库字段
if (colinfo.ffield.Substring(0, 4) =="FABC")
{
cellnew.Text = "";
}
else
{
string fvalue = type.GetProperty(colinfo.ffield).GetValue(info, null).ToString();
cellnew.Text = fvalue;
//cell 颜色控制
if (fvalue == "不合格")
{
cellnew.CellStyle = cellStyle;
}
}

}
}
row.Cells.Add(cellnew);
}
this.table_data.TableModel.Rows.Add(row);

//修改动态列的值
string fieldHour = "FABC" + info.DjTime;
int idIndex = pubtable.ColIndex(collist, fieldHour);

this.table_data.TableModel.Rows[i-1].Cells[idIndex].Text = info.dianjianResult;
if (info.dianjianResult == "不合格")
{
table_data.TableModel.Rows[i - 1].Cells[idIndex].CellStyle = cellStyle;
}
this.table_data.TableModel.Rows[i - 1].Cells[idIndex + 1].Text = info.createDate;
this.table_data.TableModel.Rows[i-1].Cells[idIndex+2].Text = info.dianjianUserName;

i = i + 1;
#endregion

}
else
{
//修改一行
string fieldHour = "FABC" + info.DjTime;
int idIndex = pubtable.ColIndex(collist, fieldHour);
this.table_data.TableModel.Rows[i-2].Cells[idIndex].Text = info.dianjianResult;
if (info.dianjianResult == "不合格")
{
table_data.TableModel.Rows[i - 2].Cells[idIndex].CellStyle = cellStyle;
}
this.table_data.TableModel.Rows[i-2].Cells[idIndex+1].Text = info.createDate;
this.table_data.TableModel.Rows[i-2].Cells[idIndex+2].Text = info.dianjianUserName;
}

}
this.table_data.EndUpdate();
}

/// <summary>
/// 查询信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnSearch_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(dtpWorkDateTo.Text.Trim()) && !string.IsNullOrEmpty(dtpWorkDateFrom.Text.Trim()))
{
if (dtpWorkDateTo.Value < dtpWorkDateFrom.Value)
{
pubfunction.ShowError("开始时刻不能大于结束时刻!");
return;
}
}
//数据查询
GetData();
}

/// <summary>
/// 导出excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnExportExcel_Click(object sender, EventArgs e)
{
//导出excel
//判断当前是否有数据信息
if (this.table_data.TableModel.Rows.Count <= 0)
{
pubfunction.ShowError("没有数据信息!");
return;
}
pubexcel.XlsOutExcel(table_data, this.Text);
}

/// <summary>
/// 关闭页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsbtnExit_Click(object sender, EventArgs e)
{
this.Close();
}

/// <summary>
/// 设置时间控件的格式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
{
DateTimePicker dtp = (DateTimePicker)sender;
dtp.Format = DateTimePickerFormat.Custom;
dtp.CustomFormat = "yyyy-MM-dd";
}

/// <summary>
/// 选择站点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnZhanDian_Click(object sender, EventArgs e)
{
Frm_ComZhandianList formedit = new Frm_ComZhandianList();
string lineCode = "";
if (this.cboLineCode.SelectedValue == null)
{
lineCode = "";
}
else
{
lineCode = this.cboLineCode.SelectedValue.ToString();
}
string lineName = this.cboLineCode.Text;

formedit.f_lineCode= lineCode;
formedit.f_lineName = lineName;

formedit.StartPosition = FormStartPosition.CenterScreen;
formedit.ShowInTaskbar = false;
formedit.ShowDialog();
if (formedit.DialogResult == DialogResult.OK)
{
txtZhandianName.Text = formedit.f_zhandianName;
lblZhandianCode.Text = formedit.f_zhandianCode;
}
formedit.Close();
formedit.Dispose();
}

/// <summary>
/// 选择岗位
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnGangwei_Click(object sender, EventArgs e)
{
#region
Frm_ComGangweiList formedit = new Frm_ComGangweiList();

string zhandianCode = "";
if (this.lblZhandianCode.Text == null||this.lblZhandianCode.Text =="")
{
zhandianCode = "";
}
else
{
zhandianCode = this.lblZhandianCode.Text;
}
string zhandianName = this.txtZhandianName.Text;

formedit.f_zhandianCode = zhandianCode;
formedit.f_zhandianName = zhandianName;

string lineCode = "";
if (this.cboLineCode.SelectedValue == null)
{
lineCode = "";
}
else
{
lineCode = this.cboLineCode.SelectedValue.ToString();
}
string lineName = this.cboLineCode.Text;

string banciCode = "";
if (this.cboBanciCode.SelectedValue == null)
{
banciCode = "";
}
else
{
banciCode = this.cboBanciCode.SelectedValue.ToString();
}
string banciName = this.cboBanciCode.Text;
formedit.lineCode = lineCode;
formedit.lineName = lineName;

formedit.StartPosition = FormStartPosition.CenterScreen;
formedit.ShowInTaskbar = false;
formedit.ShowDialog();
if (formedit.DialogResult == DialogResult.OK)
{
txtGangWeiName.Text = formedit.f_gangweiName;
lblGangweiCode.Text = formedit.f_gangweiCode;
}
formedit.Close();
formedit.Dispose();
#endregion
}

private void cboLineCode_TextChanged(object sender, EventArgs e)
{
txtZhandianName.Clear();
txtGangWeiName.Clear();
lblZhandianCode.Text = "";
lblGangweiCode.Text = "";
}

private void txtZhandianName_TextChanged(object sender, EventArgs e)
{
lblGangweiCode.Text = "";
txtGangWeiName.Clear();
}

}
时间: 2024-11-04 14:56:16

C#行列转换的相关文章

SQL行列转换6种方法

在进行报表开发时,很多时候会遇到行列转换操作,很对开发人员针对于SQL级别行列转换操作一直不甚理解,今天正好抽空对其进行了一些简单的总结.这里主要列举3种可以实现SQL行列转换的方法,包括通用SQL解法以及Oracle支持解法. 一.测试数据 测试表依旧采用Oracle经典的scott模式下的dept表和emp表,结构如下: DEPT: create table DEPT ( DEPTNO NUMBER(2) not null, DNAME VARCHAR2(14), LOC VARCHAR2(

如何用Pivot实现行列转换

在Oracle中,如果要实现行列转换,较为常见的是用DECODE和CASE语句.对于简单的行列转行,DECODE和CASE语句尚能应付.在逻辑比较复杂,分组聚合较多的场景中,DECODE和CASE语句则力有不逮.而pivot则可完美解决这一切. 首先,我们来看看Oracle对于其的解释: 可见,pivot是数据仓库中的关键技术,它利用交叉查询(crosstabulation query)将行转换为列. 基本语法如下: SELECT .... FROM <table-expr> PIVOT (

Mysql实现行列转换

前言: 最近又玩起了sql语句,想着想着便给自己出了一道题目:“行列转换”.起初瞎折腾了不少时间也上网参考了一些博文,不过大多数是采用oracle数据库当中的一些便捷函数进行处理,比如”pivot”.那么,在Mysql环境下如何处理? 自己举了个小例子: sql代码实现: 1 -- Step1:建表并插入数据 2 3 -- Step2:中间转换,即“二维转一维”得到一维表. 4 5 -- Step3:利用IF判断并“group by”即可得到目标结果表. 6 7 8 9 -- 加载 10 11

在Sqlserver下巧用行列转换日期的数据统计

在Sqlserver下巧用行列转换日期的数据统计 前言 在SQLSERVER 中有很多统计函数的基础语法,有使用Group By 或 partition by 后配合Sum,Count(*) 等用法.常应用于统计网站的PV流量.合同项目中月收入等业务场景中.在文中我分享下最近做过的统计小案例,和大家互相学习下:) 背景 合同中行项目按月收入的统计 1.业务逻辑及需求  1.1 表业务逻辑 合同是公司间互相签署的法律契约,一份合同从诞生起,就开始流转于公司的各个部门,最核心的还是盈亏的数值.盈亏是

行列转换

1 /****** Object: Table [dbo].[Table_1] Script Date: 08/06/2013 13:55:39 ******/ 2 /*行列转换*/ 3 4 SET ANSI_NULLS ON 5 GO 6 7 SET QUOTED_IDENTIFIER ON 8 GO 9 10 SET ANSI_PADDING ON 11 GO 12 13 /*对临时表无效*/ 14 IF OBJECT_ID ('#Table_1', 'U') IS NOT NULL 15

oracle行列转换总结-转载自ITPUB

原贴地址:http://www.itpub.net/thread-1017026-1-1.html 谢谢原贴大人 最近论坛很多人提的问题都与行列转换有关系,所以我对行列转换的相关知识做了一个总结, 希望对大家有所帮助,同时有何错疏,恳请大家指出, 我也是在写作过程中学习,算是一起和大家学习吧. 行列转换包括以下六种情况: *列转行 *行转列 *多列转换成字符串 *多行转换成字符串 *字符串转换成多列 *字符串转换成多行 下面分别进行举例介绍. 首先声明一点,有些例子需要如下10g及以后才有的知识

Shell练习 行列转换

原题:https://leetcode.com/problems/transpose-file/Given a text file file.txt, transpose its content. You may assume that each row has the same number of columns and each field is separated by the ' ' character. For example, if file.txt has the followin

SQL中PIVOT 行列转换

SQL中PIVOT 行列转换 本文导读:T-SQL语句中,Pivot运算符用于在列和行之间对数据进行旋转或透视转换,PIVOT命令可以实现数据表的列转行,同时执行聚合运算,UNPIVOT则与其相反,实现数据的行转列. PIVOT通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执行聚合.UNPIVOT与PIVOT执行相反的操作,将表值表达式的列转换为列值. 通俗简单的说:PIVOT就是行转列,UNPIVOT就是列传行 一.PIVOT实例 1

行列转换小结 Pivot ,Unpivot (转,改)

行专列 Pivot 1)SQL 2000版本 静态 SELECT ID , SUM(CASE Code WHEN 'Item1' THEN Value ELSE 0 END) AS Item1 , SUM(CASE Code WHEN 'Item2' THEN Value ELSE 0 END) AS Item2 , SUM(CASE Code WHEN 'Item3' THEN Value ELSE 0 END) AS Item3 FROM RowToCol GROUP BY ID --或者用

03. 行列转换写法小结

原文:03. 行列转换写法小结 行列转换,通常有2种方法,一是CASE WHEN/UNION:一是PIVOT/UNPIVOT.对于行值或列数不固定的情况,需要用动态SQL. 一. 行转列 --drop table RowToCol create table RowToCol ( ID int, Code varchar(10), Value int ) Go insert RowToCol select 1,'Item1',1000 union all select 1,'Item2',1000