C# DataGridView 与 datatable 之间数据传递

一、 DATAGRIDVIEW 数值传递给datable

int cn = dataGridView1.Rows.Count;
int col = dataGridView1.ColumnCount;
for (int j = 0; j < col;j++ )
{
DataColumn dc = new DataColumn(dataGridView1.Columns[j].Name.ToString());
dt.Columns.Add(dc);
}
for (int m = 0;m< cn; m++)
{
DataRow dr = dt.NewRow();
for (int n = 0; n < col;n++ )
{
dr[n] = Convert.ToString(dataGridView1.Rows[m].Cells[n].Value);
}

dt.Rows.Add(dr);

}

二、datatable 数值 传递到datagridview

 DataTable dt = new DataTable();

for (int x = 0; x < dt.Rows.Count; x++)
{

bool yn = Convert.ToBoolean(Convert.ToInt32(dt.Rows[x][0].ToString()));
dataGridView1.Rows.Add(yn, (x + 1).ToString(), (dt.Rows[x][2].ToString()), (dt.Rows[x][3].ToString()), (dt.Rows[x][4].ToString()));
}

猜你喜欢

转载自www.cnblogs.com/lionmxs/p/12115043.html