天天看點

C# 數組轉換為DataTable 的三個方法

轉http://www.cnblogs.com/Holmes-Jin/archive/2012/04/25/2470671.html

using System;

using System.Data;

namespace ArrayToDataTable

{

class ArrayToDataTable

{

/// <summary>

/// 把一個一維數組轉換為DataTable

/// </summary>

/// <param name="ColumnName">列名</param>

/// <param name="Array">一維數組</param>

/// <returns>傳回DataTable</returns>

/// <remarks>柳永法 http://www.yongfa365.com/ </remarks>

public static DataTable Convert(string ColumnName, string[] Array)

{

DataTable dt = new DataTable();

dt.Columns.Add(ColumnName, typeof(string));

for (int i = 0; i < Array.Length; i++)

{

DataRow dr = dt.NewRow();

dr[ColumnName] = Array[i].ToString();

dt.Rows.Add(dr);

}

return dt;

}

/// <summary>

/// 反一個M行N列的二維數組轉換為DataTable

/// </summary>

/// <param name="ColumnNames">一維數組,代表列名,不能有重複值</param>

/// <param name="Arrays">M行N列的二維數組</param>

/// <returns>傳回DataTable</returns>

/// <remarks>柳永法 http://www.yongfa365.com/ </remarks>

public static DataTable Convert(string[] ColumnNames, string[,] Arrays)

{

DataTable dt = new DataTable();

foreach (string ColumnName in ColumnNames)

{

dt.Columns.Add(ColumnName, typeof(string));

}

for (int i1 = 0; i1 < Arrays.GetLength(0); i1++)

{

DataRow dr = dt.NewRow();

for (int i = 0; i < ColumnNames.Length; i++)

{

dr[i] = Arrays[i1, i].ToString();

}

dt.Rows.Add(dr);

}

return dt;

}

/// <summary>

/// 反一個M行N列的二維數組轉換為DataTable

/// </summary>

/// <param name="Arrays">M行N列的二維數組</param>

/// <returns>傳回DataTable</returns>

/// <remarks>柳永法 http://www.yongfa365.com/ </remarks>

public static DataTable Convert(string[,] Arrays)

{

DataTable dt = new DataTable();

int a = Arrays.GetLength(0);

for (int i = 0; i < Arrays.GetLength(1); i++)

{

dt.Columns.Add("col" + i.ToString(), typeof(string));

}

for (int i1 = 0; i1 < Arrays.GetLength(0); i1++)

{

DataRow dr = dt.NewRow();

for (int i = 0; i < Arrays.GetLength(1); i++)

{

dr[i] = Arrays[i1, i].ToString();

}

dt.Rows.Add(dr);

}

return dt;

}

}

}

示例代碼:

view plaincopy to clipboardprint?

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace ArrayToDataTable

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

dataGridView1.DataSource = ArrayToDataTable.Convert("haha", new string[] { "1", "2", "3", "4", "5", "6" });

}

private void button2_Click(object sender, EventArgs e)

{

string[,] array3D = {

{ "1", "數組轉DataTable 1", "0"},

{ "2", "數組轉DataTable 2", "1"},

{ "3", "數組轉DataTable 3", "1"},

{ "4", "數組轉DataTable 4", "2"},

{ "5", "數組轉DataTable 5", "2"},

{ "6", "數組轉DataTable 6", "5"},

};

dataGridView1.DataSource = ArrayToDataTable.Convert(new string[] { "haha1", "haha2", "haha3" }, array3D);

}

private void button3_Click(object sender, EventArgs e)

{

string[,] array3D = {

{ "1", "數組轉DataTable 1", "0"},

{ "2", "數組轉DataTable 2", "1"},

{ "3", "數組轉DataTable 3", "1"},

{ "4", "數組轉DataTable 4", "2"},

{ "5", "數組轉DataTable 5", "2"},

{ "6", "數組轉DataTable 6", "5"},

};

dataGridView1.DataSource = ArrayToDataTable.Convert(array3D);

}

private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)

{

System.Diagnostics.Process.Start("Iexplore.exe", "http://www.yongfa365.com/");

}

}

}

轉載于:https://www.cnblogs.com/wnntechnology/p/5524006.html