程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成DataGridView控件行列交換的辦法

C#完成DataGridView控件行列交換的辦法

編輯:C#入門知識

C#完成DataGridView控件行列交換的辦法。本站提示廣大學習愛好者:(C#完成DataGridView控件行列交換的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成DataGridView控件行列交換的辦法正文


本文實例講述了C#完成DataGridView控件行列交換的辦法。分享給年夜家供年夜家參考。詳細以下:

該示例法式是一個Windows窗體運用法式,有閣下兩個DataGridView控件:dgvLeft和dgvRight

dgvRight除時光外的每行是dgvLeft的一列

private void Form1_Load(object sender, EventArgs e)
{
 //C#中肯定控件DataGridView依據內容主動調劑列寬長度的屬性
 //是AutoSizeColumnsMode,將它的值設置為AllCells時可以到達該後果。
 //調劑行每行頭部的寬度(可以更好地顯示出數字)
 this.dgvLeft.RowHeadersWidth = 50;
 this.dgvRight.RowHeadersWidth = 50;
 //左邊數據表
 DataTable dtLeft = new DataTable();
 dtLeft.Columns.Add("C1");
 dtLeft.Columns.Add("C2");
 dtLeft.Columns.Add("C3");
 dtLeft.Columns.Add("TIME");
 dtLeft.Rows.Add("1-1", "1-2", "1-3", DateTime.Now.ToShortTimeString());
 dtLeft.Rows.Add("2-1", "2-2", "2-3", DateTime.Now.ToShortTimeString());
 dtLeft.Rows.Add("3-1", "3-2", "3-3", DateTime.Now.ToShortTimeString());
 dtLeft.Rows.Add("4-1", "4-2", "4-3", DateTime.Now.ToShortTimeString());
 dtLeft.Rows.Add("5-1", "5-2", "5-3", DateTime.Now.ToShortTimeString());
 dtLeft.Rows.Add("6-1", "6-2", "6-3", DateTime.Now.ToShortTimeString());
 dgvLeft.DataSource = dtLeft;
 //右邊數據表,是左邊數據表的轉置
 DataTable dtRight = new DataTable();
 for (int i = 0; i < dtLeft.Rows.Count; i++)
 {
  dtRight.Columns.Add("C" + i);
 }
 dtRight.Columns.Add("TIME");
 for (int i = 0; i < dtLeft.Columns.Count - 1; i++)
 {
  object[] obj = new object[dtLeft.Rows.Count + 1];
  for (int j = 0; j < dtLeft.Rows.Count; j++)
  {
   obj[j] = dtLeft.Rows[j][i];
  }
  obj[obj.Length - 1] = DateTime.Now.ToShortTimeString();
  dtRight.Rows.Add(obj);
 }
 dgvRight.DataSource = dtRight;
}

每行的行號可以經由過程上面的辦法添加

//DataGridView 控件 dgvLeft 的 RowsAdded 事宜
private void dgvLeft_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
 for (int i = 0; i < dgvLeft.Rows.Count; i++)
 {
  this.dgvLeft.Rows[i].HeaderCell.Style.Alignment = 
   DataGridViewContentAlignment.MiddleRight;
  this.dgvLeft.Rows[i].HeaderCell.Value = i.ToString();
 }
}
//DataGridView 控件 dgvLeft 的 RowsRemoved 事宜
private void dgvLeft_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
 for (int i = 0; i < dgvLeft.Rows.Count; i++)
 {
  this.dgvLeft.Rows[i].HeaderCell.Style.Alignment =
   DataGridViewContentAlignment.MiddleRight;
  this.dgvLeft.Rows[i].HeaderCell.Value = i.ToString();
 }
}
//DataGridView 控件 dgvRight 的 RowsAdded 事宜
private void dgvRight_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
 for (int i = 0; i < dgvRight.Rows.Count; i++)
 {
  this.dgvRight.Rows[i].HeaderCell.Style.Alignment =
   DataGridViewContentAlignment.MiddleRight;
  this.dgvRight.Rows[i].HeaderCell.Value = i.ToString();
 }
}
//DataGridView 控件 dgvRight 的 RowsRemoved 事宜
private void dgvRight_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
 for (int i = 0; i < dgvRight.Rows.Count; i++)
 {
  this.dgvRight.Rows[i].HeaderCell.Style.Alignment = 
   DataGridViewContentAlignment.MiddleRight;
  this.dgvRight.Rows[i].HeaderCell.Value = i.ToString();
 }
}

運轉後果以下圖所示:

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved