风人园

弱水三千,只取一瓢,便能解渴;佛法无边,奉行一法,便能得益。
随笔 - 99, 文章 - 181, 评论 - 56, 引用 - 0
数据加载中……

c#行列转换

写了一天,做出来才知道没有那么难。
我还向我的朋友求助,向主任请教,获得了很多版本,下面我把代码列出,和大家一起分享。
我要感谢我的朋友和他的项目经理,还有我的主任。
 
第一版
#region change hang lie in DataSet out DataSet 
       
  public  DataSet changehl(DataSet mm)
  {
   try
      {
       int h=mm.Tables[0].Rows.Count;
       int l=mm.Tables[0].Columns.Count;    
       DataSet myDataSet = new DataSet();
       DataTable namesTable= new DataTable();        
       for(int i=0; i<h; i++)
       { 
        namesTable.Columns.Add(mm.Tables[0].Rows[i][0].ToString ());
        }         
       for(int i=0;i<l;i++)
       {     
        DataRow r;
        r=namesTable.NewRow();         
        for(int j=0;j<h;j++)    
       {       
        r[j] = mm.Tables[0].Rows[j][i].ToString();         
       }
        if (i==0)
        {}
        else
       {
        namesTable.Rows.Add(r); 
       }
    }
    myDataSet.Tables.Add(namesTable);      
       
    return myDataSet;
   }   
   catch(Exception ex)
   {
    return mm ;
    }
   
  }
  #endregion
 
 
第二版
 
#region change hang lie in DataGrid out DataSet 
 
private DataSet dataGridChangeRC(DataGrid ss )
  {
   DataSet myDataSet = new DataSet();
   try
   {
    int h=ss.Items.Count;
    int l=ss.Items[0].Cells.Count;
    string[,] ary=new string[h,l];
    for(int i=0;i<h;i++)
    {
     for(int j=0;j<l;j++)
     {
      ary[i,j]=ss.Items[i].Cells[j].Text.Trim().ToString () ;
     }
    }
    DataTable namesTable= new DataTable(); 
    for(int i=0; i<h; i++)
    { 
     namesTable.Columns.Add(ary[i,0]);
    } 
    for(int i=0;i<l;i++)
    {     
     DataRow r;
     r=namesTable.NewRow();         
     for(int j=0;j<h;j++)    
     {       
      r[j] = ary[j,i];       
     }
     if (i==0)
     {}
     else
     {
      namesTable.Rows.Add(r); 
     }
    }
    myDataSet.Tables.Add(namesTable);      
    return myDataSet;
   }
   catch(Exception ex)
   {
       return myDataSet;
   }
  }
 #endregion
 
第三版

  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  namespace Win_form
  {
     public partial class DataSet : Form
         {
          public DataSet()
          {
           InitializeComponent();
          }
          #region change hang lie

          public static DataSet changehl(DataSet mm)
          {
           try
           {
            int sourceRowsCount = mm.Tables[0].Rows.Count;
            int sourceColumnsCount = mm.Tables[0].Columns.Count;
            DataSet destinationDataSet = new DataSet();
            DataTable destinationTable = new DataTable();
            #region initialize destination table
            for (int i = 0; i < sourceRowsCount; i++)
            {
            destinationTable.Columns.Add(Convert.ToString(i));
            }
            destinationTable.Rows.Count = sourceColumnsCount;
            #endregion
            #region reverse source table to destination table Using Two Dimension Matrix Reverse Arithmetic
            int dcIndex;//the column number of each destination table row
            int srIndex;//the row number of each source table column
            int drIndex;
            int scIndex;
            for (dcIndex = 0, srIndex = 0;
             dcIndex < destinationTable.Columns.Count
             && srIndex < sourceRowsCount; dcIndex++, srIndex++)
            {
             for (drIndex = 0,scIndex=0;
              drIndex < destinationTable.Rows.Count
              && scIndex < sourceColumnsCount; drIndex++, scIndex++)
             {
              destinationTable.Rows[drIndex][dcIndex] = mm.Tables[0].Rows[srIndex][scIndex];
             }
            }
            #endregion
            destinationDataSet.Tables.Add(destinationTable);
            return destinationDataSet;
           }
           catch (Exception ex)
           {
            MessageBox.Show(ex.ToString());
           }
          }
          #endregion
         }
  }
 
第四版
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DataSetConvert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void RowtoCollumn()
        {
            DataSet sourceDataSet = new DataSet();
            DataSet destinationDataSet = new DataSet();
            int sourceRowCount = sourceDataSet.Tables[0].Rows.Count;
            int destinationColCount = sourceRowCount;
            int sourceColCount = sourceDataSet.Tables[0].Columns.Count;
            int destinationRowCount = sourceColCount;
            for(int i=0;i<destinationRowCount;i++)
                    for (int j=0; j < destinationColCount; j++)
                    {
                        destinationDataSet.Tables[0].Rows[i][j]=sourceDataSet.Tables[0].Rows[j][i];
                    }  
        
         }
    }
}
 
第一版和第二版是基于 web做的第一版可以做为class和dll 第二版只可以做class 我试过生成dll 不行 第三版和第四版 是我朋友和他的项目经理做的 是在windows form 中做的

posted on 2007-03-09 10:03 风人园 阅读(1358) 评论(0)  编辑  收藏 所属分类: DotNet