C#中使用DataGridView显示二维数组中的内容

https://blog.csdn.net/jasonleesjtu/article/details/7555514

https://zhidao.baidu.com/question/1759602605808979508.html

int[,] TABLE = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
            DataTable dt = new DataTable();
//获取数据列数 for (int i = 0; i < TABLE.GetLength(1); i++) dt.Columns.Add(i.ToString(), typeof(int));
//获取数据行数 for (int i = 0; i < TABLE.GetLength(0); i++) { DataRow dr = dt.NewRow(); for (int j = 0; j < TABLE.GetLength(1); j++) dr[j] = TABLE[i, j]; dt.Rows.Add(dr); } this.dataGridView1.DataSource = dt;

private void Form1_Load(object sender, EventArgs e)
{
string[,] array=new string[5,4]; //5行4列二维数组
//对数组赋值示例,以便图形显示,为方便用数字代表字符串,
for (int i = 0; i < 5; i++)
for (int j = 0; j < 4; j++)
array[i, j] = (i * 4 + j).ToString();
List<string[]> str = new List<string[]>(); //声明泛型数组
//将二维数组的内容添加到泛型数组
string[] st = new string[4];
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
{
st[j] = array[i, j];
}
str.Add(st);
}
//查询
var Query = from s in str
select new
{
第1列 = s[0],
第2列 = s[1],
第3列 = s[2],
第4列 = s[3]
};
this.bindingSource1.DataSource = Query;
this.dataGridView1.DataSource = this.bindingSource1;
}
posted @   yinghualeihenmei  阅读(309)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示