分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 软件开发

Asp.net GridView转换成DataTable

发布时间:2023-09-06 02:17责任编辑:熊小新关键词:暂无标签

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

 ?1 ?#region ================GridView转DataTable方法================ ?2 ????????/// <param name="gv">已绑定数据源的GridView</param> ?3 ????????/// <param name="showHideColumn">是否显示隐藏列</param> ?4 ????????/// <returns>DataTable</returns> ?5 ????????public static DataTable GridViewToDataTable(GridView gv, Boolean showHideColumn) ?6 ????????{ ?7 ????????????//处理后的数据表 ?8 ????????????DataTable dt = new DataTable(); ?9 ??10 ????????????//记录符合条件索引 11 ????????????int[] columnIndexs = new int[gv.HeaderRow.Cells.Count]; 12 ????????????//记录指示器从0开始 13 ????????????int columnIndexsCount = 0; 14 ??15 ????????????//初始化dt列名 16 ????????????for (int i = 0; i < gv.HeaderRow.Cells.Count; i++) 17 ????????????{ 18 ????????????????//获取列名 19 ????????????????string columnName = GetCellText(gv.HeaderRow.Cells[i]); 20 ????????????????//string columnName = gv.HeaderRow.Cells[i].Text; 21 ??22 ????????????????//列名非空//且可见 23 ????????????????if (!string.IsNullOrEmpty(columnName)) 24 ????????????????{ 25 ????????????????????//是否显示隐藏列 26 ????????????????????if (gv.HeaderRow.Cells[i].Visible || showHideColumn) 27 ????????????????????{ 28 ????????????????????????//列名不允许重复 29 ????????????????????????if (!dt.Columns.Contains(columnName)) 30 ????????????????????????{ 31 ????????????????????????????//dt中新增一列 32 ????????????????????????????DataColumn dc = dt.Columns.Add(); 33 ????????????????????????????//列名 34 ????????????????????????????dc.ColumnName = columnName; 35 ????????????????????????????//存储的数据类型 36 ????????????????????????????dc.DataType = typeof(string); 37 ??38 ????????????????????????????//记录符合条件的列索引 39 ????????????????????????????columnIndexs[columnIndexsCount] = i; 40 ????????????????????????????//记录指示器+1 41 ????????????????????????????columnIndexsCount++; 42 ????????????????????????} 43 ????????????????????} 44 ????????????????} 45 ????????????} 46 ??47 ????????????//GridView行复制到数组中便于操作 48 ????????????GridViewRow[] allGridViewRow = new GridViewRow[gv.Rows.Count]; 49 ????????????gv.Rows.CopyTo(allGridViewRow, 0); 50 ??51 ????????????//数据添加到dt中 52 ????????????foreach (GridViewRow row in allGridViewRow) 53 ????????????{ 54 ????????????????//创建一行 55 ????????????????DataRow dr = dt.NewRow(); 56 ????????????????//符合条件的列 57 ????????????????for (int i = 0; i < columnIndexsCount; i++) 58 ????????????????{ 59 ????????????????????//获取显示文本并保存 60 ????????????????????dr[i] = GetCellText(row.Cells[columnIndexs[i]]); 61 ????????????????} 62 ????????????????//dt中增加此行 63 ????????????????dt.Rows.Add(dr); 64 ????????????} 65 ????????????//返回处理后的数据 66 ????????????return dt; 67 ????????} 68 ??69 ????????/// <param name="gv">未绑定数据源的GridView</param> 70 ????????/// <param name="dtSource">GridView的数据源</param> 71 ????????/// <param name="showHideColumn">是否显示隐藏列</param> 72 ????????/// <returns>DataTable</returns> 73 ????????public static DataTable GridViewToDataTable(GridView gv, DataTable dtSource, Boolean showHideColumn) 74 ????????{ 75 ????????????//绑定原始数据到GridView 76 ????????????gv.DataSource = dtSource; 77 ????????????gv.DataBind(); 78 ????????????//设置为不分页 79 ????????????gv.AllowPaging = false; 80 ????????????//GridView转DataTable并返回 81 ????????????return GridViewToDataTable(gv, showHideColumn); 82 ????????} 83 ????????#endregion 84 ??85 ????????#region ================私有工具方法================ 86 ????????/// <param name="cell">TableCell</param> 87 ????????/// <returns>string</returns> 88 ????????private static string GetCellText(TableCell cell) 89 ????????{ 90 ????????????string cellText = cell.Text; 91 ????????????//常规文本(无控件)直接返回 92 ????????????if (!string.IsNullOrEmpty(cellText)) 93 ????????????{ 94 ????????????????//返回显示文本 95 ????????????????return cellText.Replace(" ", ""); 96 ????????????} 97 ????????????//遍历cell中的控件 98 ????????????foreach (Control control in cell.Controls) 99 ????????????{100 ????????????????if (control != null && control is IButtonControl)101 ????????????????{102 ????????????????????IButtonControl btn = control as IButtonControl;103 ????????????????????cellText += btn.Text.Replace("\r\n", "").Trim();104 ????????????????????continue;105 ????????????????}106 ????????????????if (control != null && control is ITextControl)107 ????????????????{108 ????????????????????LiteralControl lc = control as LiteralControl;109 ????????????????????if (lc != null)110 ????????????????????{111 ????????????????????????//跳出到下一步foreach112 ????????????????????????continue;113 ????????????????????}114 ????????????????????ITextControl l = control as ITextControl;115 ?116 ????????????????????cellText += l.Text.Replace("\r\n", "").Trim();117 ????????????????????continue;118 ????????????????}119 ????????????}120 ????????????//返回显示文本121 ????????????return cellText;122 ????????}123 ????????#endregion
 1 ??#region ================另一种方法================ 2 ????????public static DataTable GetGridDataTable(GridView grid) 3 ????????{ 4 ????????????DataTable dt = new DataTable(); 5 ????????????DataColumn dc;//创建列 ?6 ????????????DataRow dr; ??????//创建行 ?7 ????????????//构造列 ?8 ????????????for (int i = 0; i < grid.Columns.Count; i++) 9 ????????????{10 ????????????????dc = new DataColumn();11 ????????????????dc.ColumnName = grid.Columns[i].HeaderText;12 ????????????????dt.Columns.Add(dc);13 ????????????}14 ????????????//构造行 15 ????????????for (int i = 0; i < grid.Rows.Count; i++)16 ????????????{17 ????????????????dr = dt.NewRow();18 ????????????????for (int j = 0; j < grid.Columns.Count; j++)19 ????????????????{20 ????????????????????dr[j] = grid.Rows[i].Cells[j].Text;21 ????????????????}22 ????????????????dt.Rows.Add(dr);23 ????????????}24 25 ????????????return dt;26 ????????}27 ????????#endregion

Asp.net GridView转换成DataTable

原文地址:https://www.cnblogs.com/lgx5/p/9748417.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved