将GridView转换成DataTable

来源:网络 作者:网友
们一般是将DataTable绑定到GridView中,但有时候我们确实需要将数据从GridView转换成DataTable,我们可以根据两者的相似性,实现从GridView生成DataTable,下面为实现代码:

/**//*----------------------------------------------------------------
// Copyright (C) 2007 jillzhang 版权所有。
//
// 文件名:GridView.cs
// 文件功能描述:
//
// 创建标识:jillzhang
// 修改标识:
// 修改描述:
//
// 修改标识:
// 修改描述:
//----------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI;


namespace jzlib.Common
{
public class GridViewHelper
{
public static string GetCellText(TableCell cell)
{
string text = cell.Text;
if (!string.IsNullOrEmpty(text))
{
return text;
}
foreach (Control control in cell.Controls)
{
if (control != null && control is IButtonControl)
{
IButtonControl btn = control as IButtonControl;
text = btn.Text.Replace("\r\n", "").Trim();
break;
}
if (control != null && control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
if (lc != null)
{
continue;
}
ITextControl l = control as ITextControl;

text = l.Text.Replace("\r\n", "").Trim();
break;
}
}
return text;
}
/**//// <summary>
/// 从GridView的数据生成DataTable
/// </summary>
/// <param name="gv">GridView对象</param>
public static DataTable GridView2DataTable(GridView gv)
{
DataTable table = new DataTable();
int rowIndex = 0;
List<string> cols = new List<string>();
if (!gv.ShowHeader && gv.Columns.Count == 0)
{
return table;
}
GridViewRow headerRow = gv.HeaderRow;
int columnCount = headerRow.Cells.Count;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(headerRow.Cells[i]);
cols.Add(text);
}
foreach (GridViewRow r in gv.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
DataRow row = table.NewRow();
int j = 0;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(r.Cells[i]);
if (!String.IsNullOrEmpty(text))
{
if (rowIndex == 0)
{
string columnName = cols[i];
if (String.IsNullOrEmpty(columnName))
{
continue;
}
if (table.Columns.Contains(columnName))
{
continue;
}
DataColumn dc = table.Columns.Add();
dc.ColumnName = columnName;
dc.DataType = typeof(string);
}
row[j] = text;
j++;
}
}
rowIndex++;
table.Rows.Add(row);
}
}
return table;
}
}
}

使用这个函数,您可以得到GridView当前页面的数据,当然如果您在GridView中添加了复杂的控件,我们将略过这些内容,我们只从显示文本的列中导出数据到DataTable,如果您想导出GridView的全部数据,请在绑定前设置AllowPaging=false;
当然大家会问这样做有什么用呢?这个函数是我在开发将GridView导出Excel的时候想到的,类似于这种应用情形,我想还有很多。
下面是一个示例,通过从上面的GridView导出DataTable,然后再绑定到下面的GridView中
 

相关文档推荐

人工智能技术发展与应用实践.PDF

1743586449 史树明 5.88MB 35页 积分6

DeepSeek行业应用案例集解锁.PDF

1743586338  5.03MB 152页 积分6

2025AI大模型产业市场前景及投资研究报告.PDF

1743586288  4.47MB 22页 积分0

AI韧性AI安全的革命性基准模型.PDF

1743586206  1.91MB 38页 积分4

Deepseek大模型生态报告.PDF

1743586174 赛迪 1.26MB 149页 积分6

DeepSeek在金融银行的应用方案.PDF

1743586138  1.12MB 146页 积分6

AI为中心的数字化转型战略理解与落地安排.PDF

1743586083 侯宏 1.42MB 16页 积分5

深度解读 DeepSeek 部署、使用、安全.PDF

1743586019 天津大学 5.47MB 46页 积分6

2025中国AI Agent 营销市场发展潜力研究报告.PDF

1743585964  6.55MB 25页 积分5

Deepseek技术全景解析.PDF

1743585886  6.52MB 47页 积分6

相关文章推荐