栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

Asp.net静态方法之Grid转DataTable方法实现步骤

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Asp.net静态方法之Grid转DataTable方法实现步骤

GridView绑定DataTable后,如何获取GridView绑定后显示的值,在项目需求需要的背景下,搜索了获取单元格显示文本的方法,然后写了一个静态方法,经过在项目中的使用,bug的修复,较为稳定。

独乐乐不如众乐乐,把代码贴出来供大家指正。
复制代码 代码如下:
#region ================GridView转DataTable方法================
/// GridView转DataTable 版权:求知域http://www.qqextra.com,http://blog.csdn.net/ls_man转载请注明出处
/// 已绑定数据源的GridView
/// 是否显示隐藏列
/// DataTable
public static DataTable GridViewToDataTable(GridView gv, Boolean showHideColumn)
{
//处理后的数据表
DataTable dt = new DataTable();
//记录符合条件索引
int[] columnIndexs = new int[gv.HeaderRow.Cells.Count];
//记录指示器从0开始
int columnIndexsCount = 0;
//初始化dt列名
for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
{
//获取列名
string columnName = GetCellText(gv.HeaderRow.Cells[i]);
//string columnName = gv.HeaderRow.Cells[i].Text;
//列名非空//且可见
if (!string.IsNullOrEmpty(columnName))
{
//是否显示隐藏列
if (gv.HeaderRow.Cells[i].Visible || showHideColumn)
{
//列名不允许重复
if (!dt.Columns.Contains(columnName))
{
//dt中新增一列
DataColumn dc = dt.Columns.Add();
//列名
dc.ColumnName = columnName;
//存储的数据类型
dc.DataType = typeof(string);
//记录符合条件的列索引
columnIndexs[columnIndexsCount] = i;
//记录指示器+1
columnIndexsCount++;
}
}
}
}//版权:求知域http://www.qqextra.com,http://blog.csdn.net/ls_man转载请注明出处
//GridView行复制到数组中便于操作
GridViewRow[] allGridViewRow = new GridViewRow[gv.Rows.Count];
gv.Rows.CopyTo(allGridViewRow, 0);
//数据添加到dt中
foreach (GridViewRow row in allGridViewRow)
{
//创建一行
DataRow dr = dt.NewRow();
//符合条件的列
for (int i = 0; i < columnIndexsCount; i++)
{
//获取显示文本并保存
dr[i] = GetCellText(row.Cells[columnIndexs[i]]);
}
//dt中增加此行
dt.Rows.Add(dr);
}
//返回处理后的数据
return dt;
}
/// GridView转DataTable 版权:求知域http://www.qqextra.com,http://blog.csdn.net/ls_man转载请注明出处
/// 未绑定数据源的GridView
/// GridView的数据源
/// 是否显示隐藏列
/// DataTable
public static DataTable GridViewToDataTable(GridView gv, DataTable dtSource, Boolean showHideColumn)
{
//绑定原始数据到GridView
gv.DataSource = dtSource;
gv.DataBind();
//设置为不分页
gv.AllowPaging = false;//版权:求知域http://www.qqextra.com,http://blog.csdn.net/ls_man转载请注明出处
//GridView转DataTable并返回
return GridViewToDataTable(gv, showHideColumn);
}
#endregion
#region ================私有工具方法================
/// 获取TableCell的显示文本 版权:求知域http://www.qqextra.com,http://blog.csdn.net/ls_man转载请注明出处
/// TableCell
/// string
private static string GetCellText(TableCell cell)
{
string cellText = cell.Text;
//常规文本(无控件)直接返回
if (!string.IsNullOrEmpty(cellText))
{
//返回显示文本
return cellText.Replace(" ", "");
}
//遍历cell中的控件
foreach (Control control in cell.Controls)
{
if (control != null && control is IButtonControl)
{
IButtonControl btn = control as IButtonControl;
cellText += btn.Text.Replace("rn", "").Trim();
continue;
}版权:求知域http://www.qqextra.com,http://blog.csdn.net/ls_man转载请注明出处
if (control != null && control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
if (lc != null)
{
//跳出到下一步foreach
continue;
}
ITextControl l = control as ITextControl;
cellText += l.Text.Replace("rn", "").Trim();
continue;
}
}
//返回显示文本
return cellText;
}
#endregion

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/57832.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号