using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsApplication44 { public partial class Form1 : Form { public Form1() { InitializeComponent(); DataTable DT = new DataTable(); DT.Columns.Add("c1", typeof(int)); DT.Columns.Add("c2", typeof(int)); DT.Columns.Add("c3", typeof(int)); DT.Rows.Add(new Object[] { 1, 2, 3 }); DT.Rows.Add(new Object[] { 11, 22, 33 }); DT.Rows.Add(new Object[] { 111, 222, 333 }); DataGridView DGV = new DataGridView(); DGV.Parent = this; DataGridViewTextBoxColumn Column = new DataGridViewTextBoxColumn(); Column.DataPropertyName = "c1"; DGV.Columns.Add(Column); Column = new DataGridViewTextBoxColumn(); Column.DataPropertyName = "c2"; DGV.Columns.Add(Column); Column = new DataGridViewTextBoxColumn(); Column.DataPropertyName = "c3"; DGV.Columns.Add(Column); DGV.DataSource = DT; int[][] A = new int[DT.Rows.Count][]; for (int i = 0; i < DT.Rows.Count; i++) DT.Rows[i].ItemArray.CopyTo(A[i] = new int[DT.Columns.Count], 0); String S = String.Empty; for (int i = 0; i < A.Length; i++) for (int j = 0; j < A[i].Length; j++) S += A[i][j].ToString() + " "; MessageBox.Show(S); // 1 2 3 11 22 33 111 222 333 } } }
你的Gridview里不可能都是数字吧,如果有文字就不能用int数组了using System; using System.Collections.Generic; using System.Linq; using System.Text; using ClassLibrary; using System.Data;namespace ConsoleApplication { class Program { static void Main(string[] args) { DataTable table = new DataTable();//创建Gridview数据源 table.Columns.Add(new DataColumn("ID",typeof(Int32))); table.Columns.Add(new DataColumn("Name", typeof(String))); table.Rows.Add(1,"aaa"); table.Rows.Add(2, "bbb"); table.Rows.Add(3, "ccc"); StInfo[] arr = new StInfo[3];//用结构体存放Gridview行数据 StInfo yang; int i = 0; foreach (DataRow row in table.Rows) { yang = new StInfo(); yang.id = int.Parse(row[0].ToString()); yang.name = row[1].ToString(); arr[i] = yang; i++; } for (i = 0; i < arr.Length; i++) { Console.WriteLine("Id:{0},Name:{1}", arr[i].id.ToString(), arr[i].name); } Console.ReadKey(); } } struct StInfo { public int id; public string name; } }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication44
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); DataTable DT = new DataTable();
DT.Columns.Add("c1", typeof(int));
DT.Columns.Add("c2", typeof(int));
DT.Columns.Add("c3", typeof(int));
DT.Rows.Add(new Object[] { 1, 2, 3 });
DT.Rows.Add(new Object[] { 11, 22, 33 });
DT.Rows.Add(new Object[] { 111, 222, 333 }); DataGridView DGV = new DataGridView();
DGV.Parent = this;
DataGridViewTextBoxColumn Column = new DataGridViewTextBoxColumn();
Column.DataPropertyName = "c1";
DGV.Columns.Add(Column);
Column = new DataGridViewTextBoxColumn();
Column.DataPropertyName = "c2";
DGV.Columns.Add(Column);
Column = new DataGridViewTextBoxColumn();
Column.DataPropertyName = "c3";
DGV.Columns.Add(Column);
DGV.DataSource = DT; int[][] A = new int[DT.Rows.Count][]; for (int i = 0; i < DT.Rows.Count; i++)
DT.Rows[i].ItemArray.CopyTo(A[i] = new int[DT.Columns.Count], 0); String S = String.Empty;
for (int i = 0; i < A.Length; i++)
for (int j = 0; j < A[i].Length; j++)
S += A[i][j].ToString() + " "; MessageBox.Show(S); // 1 2 3 11 22 33 111 222 333
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary;
using System.Data;namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
DataTable table = new DataTable();//创建Gridview数据源
table.Columns.Add(new DataColumn("ID",typeof(Int32)));
table.Columns.Add(new DataColumn("Name", typeof(String)));
table.Rows.Add(1,"aaa");
table.Rows.Add(2, "bbb");
table.Rows.Add(3, "ccc"); StInfo[] arr = new StInfo[3];//用结构体存放Gridview行数据
StInfo yang;
int i = 0;
foreach (DataRow row in table.Rows)
{
yang = new StInfo();
yang.id = int.Parse(row[0].ToString());
yang.name = row[1].ToString();
arr[i] = yang;
i++;
} for (i = 0; i < arr.Length; i++)
{
Console.WriteLine("Id:{0},Name:{1}", arr[i].id.ToString(), arr[i].name);
} Console.ReadKey();
}
} struct StInfo
{
public int id;
public string name;
}
}
http://blog.csdn.net/flygoldfish/archive/2006/05/16/741351.aspx