風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 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 風人園 閱讀(1379) 評論(0)  編輯  收藏 所屬分類: DotNet

          主站蜘蛛池模板: 梁山县| 绩溪县| 会东县| 曲阜市| 四川省| 阿拉善右旗| 南汇区| 乐东| 镇江市| 郸城县| 布拖县| 彩票| 西城区| 翼城县| 镇江市| 岱山县| 兴化市| 隆尧县| 德惠市| 普洱| 永川市| 怀集县| 北安市| 秦安县| 济南市| 新闻| 叙永县| 徐水县| 田林县| 呼伦贝尔市| 岢岚县| 梅州市| 遵义县| 罗城| 本溪市| 洛宁县| 西林县| 和硕县| 徐州市| 萍乡市| 修武县|