在C#中实现行列转换

目标

在程序中实现行列转换

效果

 


实现流程图

 

 

测试用的XML文件

<? xml version="1.0" encoding="utf-8"  ?>
< table >
    
< tr >
        
< 编码 > A01 </ 编码 >
        
< 内容 > </ 内容 >
        
< M0601 > 1 </ M0601 >
        
< M0602 > 2 </ M0602 >
    
</ tr >
    
< tr >
        
< 编码 > A02 </ 编码 >
        
< 内容 > </ 内容 >
        
< M0601 > 3 </ M0601 >
        
< M0602 > 4 </ M0602 >
    
</ tr >
</ table >


实现程序

<% @ Page language="c#" Codebehind="RowColumnConvert.aspx.cs" AutoEventWireup="false" Inherits="DataGridTest.RowColumnConvert"  %>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
< HTML >
    
< HEAD >
        
< title > WebForm2 </ title >
        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
    
</ HEAD >
    
< body  MS_POSITIONING ="GridLayout" >
        
< form  id ="Form1"  method ="post"  runat ="server" >
            
< FONT  face ="宋体" >
                
< asp:DataGrid  id ="dgOld"  style ="Z-INDEX: 101; LEFT: 72px; POSITION: absolute; TOP: 48px"  runat ="server"
                    Width
="312px"  Height ="88px" ></ asp:DataGrid >
                
< asp:DataGrid  id ="dgNew"  style ="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 272px"  runat ="server"
                    Width
="256px"  Height ="144px" ></ asp:DataGrid >
                
< asp:Button  id ="btnChange"  style ="Z-INDEX: 103; LEFT: 168px; POSITION: absolute; TOP: 208px"
                    runat
="server"  Width ="72px"  Height ="32px"  Text ="转换" ></ asp:Button >
                
< asp:Label  id ="Label1"  style ="Z-INDEX: 104; LEFT: 536px; POSITION: absolute; TOP: 136px"  runat ="server"
                    Width
="136px"  Height ="40px" > Label </ asp:Label ></ FONT >
        
</ form >
    
</ body >
</ HTML >

 

/***********************************************************************
 * Module:  RowColumnConvert.cs
 * Author:  skyendless
 * Purpose: Definition of the Class RowColumnConvert
 ***********************************************************************/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace DataGridTest
{
    /// 
< summary >
    /// WebForm2 的摘要说明。
    /// 
</ summary >
    public class RowColumnConvert : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid dgOld;
        protected System.Web.UI.WebControls.DataGrid dgNew;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.Button btnChange;
    
        DataSet ds = new DataSet();
        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            try
            {
                //
                //具体路径根据自己的情况进行修改
                //
                ds.ReadXml(@"C:/Inetpub/wwwroot/DataGridTest/test.xml");
                dataBind(this.dgOld);
            }
            catch(Exception ex)
            {
                this.Label1.Text = ex.Message.ToString ();
            }
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }
        
        /// 
< summary >
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// 
</ summary >
        private void InitializeComponent()
        {    
            this.btnChange.Click += new System.EventHandler(this.Button1_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            this.dgNew.DataSource = DataConvert(ds,2,3).Tables[0].DefaultView;
            this.dgNew.DataBind();
             
        }
        private void dataBind(DataGrid dg)
        {
            dg.DataSource = ds.Tables[0].DefaultView;
            dg.DataBind();
        }

        /// 
< summary >
        /// 行列转换
        /// 
</ summary >
        /// 
< param  name ="ds" > 要转换的数据源 </ param >
        /// 
< param  name ="startIndex" > 起始字段索引 </ param >
        /// 
< param  name ="endIndex" > 终止字段索引 </ param >
        /// 
< returns ></ returns >
        private DataSet DataConvert(DataSet ds,int startIndex,int endIndex)
        {
            DataSet dsNew = new DataSet();
            try
            { 
                for(int i = startIndex;i
< =endIndex ;i++)
                {
                    DataTable dtPart 
= ds.Tables[0].Copy();//取数据源一个拷贝
                    
for(int j  = startIndex;j<=endIndex;j++)
                    
{
                        if(i!
=j)
                        
{
                            //
                            //去掉不是要取出的字段
                            //
                            dtPart.Columns.Remove(dtPart.Columns[j].ColumnName);  
                        }
                    }
                    
                    //
                    //增加一个字段
                    //
                    DataColumn dc 
= new  DataColumn ("月份",System.Type.GetType("System.String"));
                    dc.Expression 
=  "'" +dtPart.Columns[startIndex].ColumnName+"'";  //定义默认值
                    dtPart.Columns.Add(dc);

                    dtPart.Columns[startIndex].ColumnName 
= "数量" ;//修改字段名

                    if(dsNew.Tables.Count 
>  0)
                    {
                        
                        dsNew.Merge(dtPart);  //合并
                    }
                    else
                    {
                        dsNew.Tables.Add(dtPart);
                    }
                }
            }
            catch(Exception ex)
            {
                this.Label1.Text = ex.Message.ToString();
            }

            return dsNew;
        }
    }
}


你可能感兴趣的:(在C#中实现行列转换)