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;
}
浙公网安备 33010602011771号