110,536
社区成员
发帖
与我相关
我的任务
分享
--建立测试环境
create table materals
(id int identity(1,1),
step_number int ,
dept_code varchar(20),
dept_name nvarchar(50),
parameter_desc nvarchar(200),
parameter_value nvarchar(20) )
insert into materals
select '1','c05','cc','length','427.44*586.40mm' union
select '1','c05','cc','depath','2.5+/-0.25mm' union
select '1','c05','cc','other','no' union
select '2','p01','bb','bbname','single' union
select '2','p01','bb','bbVision','DSR330'
--结束
--运行下列递归查询
with x(id,step_number,dept_code,dept_name,descn,cnt,length)
as
(
select id,step_number,dept_code,dept_name,
cast(parameter_desc+':'+parameter_value as varchar(400) )as descn ,
count(*) over (partition by step_number) as cnt,1 as length
from materals
union all
select r.id, r.step_number,r.dept_code,r.dept_name,
cast(x.descn+'<br />'+r.parameter_desc+':'+r.parameter_value as varchar(400)),
x.cnt,x.length+1
from materals r join x on r.step_number=x.step_number and r.id>x.id
)
select step_number as '序号',dept_code as '工序',descn as '说明' from x where length=cnt
--结束
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication119
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable DT = new DataTable();
DT.Columns.Add("c1");
DT.Columns.Add("c2");
DT.Columns.Add("c3");
DT.Rows.Add(new Object[] { 1, 2, 3 });
DT.Rows.Add(new Object[] { 11, 22, 33 });
DataGridView DGV = new DataGridView();
DGV.Parent = this;
DGV.Dock = DockStyle.Fill;
DGV.Columns.Add(new DataGridViewTextBoxColumn());
DGV.CellFormatting += new DataGridViewCellFormattingEventHandler(DGV_CellFormatting);
DGV.AutoGenerateColumns = false;
DGV.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
DGV.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
DGV.DataSource = DT;
}
void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
DataGridView DGV = (DataGridView)sender;
DataTable DT = (DataTable)DGV.DataSource;
if (e.RowIndex < DT.Rows.Count)
e.Value = DT.Rows[e.RowIndex]["c1"].ToString() + Environment.NewLine
+ DT.Rows[e.RowIndex]["c2"].ToString() + Environment.NewLine
+ DT.Rows[e.RowIndex]["c3"].ToString();
}
}
}