博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数组转DataTable
阅读量:5757 次
发布时间:2019-06-18

本文共 2741 字,大约阅读时间需要 9 分钟。

using System;
using System.Data;
namespace ArrayToDataTable
{
class ArrayToDataTable
{
/// <summary>
/// 把一个一维数组转换为DataTable
/// </summary>
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>
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>
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;
}
}
}
调用
[csharp] view plaincopyprint?
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/NetPig/p/3962084.html

你可能感兴趣的文章
纯jfinal实现,数据库表自动创建实体类
查看>>
mysql 笔记
查看>>
Supervisor&Gunicorn&Django
查看>>
1.8 uniq和tee命令
查看>>
修改keystore条目的密码
查看>>
Spring Boot 学习笔记2 - Spring Bean 和依赖
查看>>
分享到微信微博空间等第三方平台的JS代码
查看>>
python装饰器合并
查看>>
fiddler2抓包工具使用图文教程
查看>>
作为开发者,你不应该害怕的8件事
查看>>
Cookie跨域操作
查看>>
Mybaties批量更新
查看>>
ubuntu14.04 安装HAXM(KVM)提升android虚拟机Android x8运行速度
查看>>
SpringMVC4 + Hibernate4 整合, 使用Java配置
查看>>
nginx负载均衡简单配置
查看>>
xib和storyboard的不同点总结
查看>>
对Android Handler Message Looper常见用法,知识点的一些总结
查看>>
大数据最佳学习路线
查看>>
JavaScript学习笔记01——关键字this
查看>>
IOS--GPS闹钟离线版--01toolBar添加退出程序按钮
查看>>