public class GridViewHelper
{
/// <summary>
/// 从GridView的数据生成DataTable
/// </summary>
/// <param name="gv">GridView对象</param>
public static DataTable GridView2DataTable(GridView gv)
{
DataTable table = new DataTable();
if (!gv.ShowHeader && gv.Columns.Count == 0)
{
return table;
}
int columnCount = gv.Columns.Count;
for (int i = 0; i < columnCount; i++)
{
string text =gv.Columns[i].HeaderText;
table.Columns.Add(text);//往table中添加列
}
foreach (GridViewRow r in gv.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
DataRow row = table.NewRow();
for (int i = 0; i < columnCount; i++)
{
string text = r.Cells[i].Text;
if (!String.IsNullOrEmpty(text))
{
row[i] = text;//为行赋值
}
}
table.Rows.Add(row);
}
}
return table;
}
}
{
/// <summary>
/// 从GridView的数据生成DataTable
/// </summary>
/// <param name="gv">GridView对象</param>
public static DataTable GridView2DataTable(GridView gv)
{
DataTable table = new DataTable();
if (!gv.ShowHeader && gv.Columns.Count == 0)
{
return table;
}
int columnCount = gv.Columns.Count;
for (int i = 0; i < columnCount; i++)
{
string text =gv.Columns[i].HeaderText;
table.Columns.Add(text);//往table中添加列
}
foreach (GridViewRow r in gv.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
DataRow row = table.NewRow();
for (int i = 0; i < columnCount; i++)
{
string text = r.Cells[i].Text;
if (!String.IsNullOrEmpty(text))
{
row[i] = text;//为行赋值
}
}
table.Rows.Add(row);
}
}
return table;
}
}