Asp.net GridView转换成DataTable

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                         //跳出到下一步foreach
112                         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

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

时间: 2024-11-16 14:14:50

Asp.net GridView转换成DataTable的相关文章

list 转换成datatable

感谢网上的一位朋友 1 /// <summary> 2 60 /// 将集合类转换成DataTable 3 61 /// </summary> 4 62 /// <param name="list">集合</param> 5 63 /// <returns></returns> 6 64 public static DataTable ToDataTable(IList list) 7 65 { 8 66 Data

将list&lt;对象&gt;转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据

领导让在存储过程中批量添加数据,找出效率最高的,我看到后台代码后,发现可以将list<对象>转换成DataTable,把DataTable转换成参数传入存储过程实现批量插入数据,知道还有其他的方法,不过这个方法已经实现,就写一下了: 1.创建表. CREATE TABLE [dbo].[person]( [ID] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](50) NULL, [Pwd] [nvarchar](50) NULL, [Age]

C# DataTable转换成实体列表 与 实体列表转换成DataTable

/// <summary> /// DataTable转换成实体列表 /// </summary> /// <typeparam name="T">实体 T </typeparam> /// <param name="table">datatable</param> /// <returns></returns> public static IList<T>

.net excel 转换成datatable,创建文件夹

protected void Button9_Click(object sender, EventArgs e) { string path = ""; path = FileUpload3.PostedFile.FileName; if (path == "") { string jss = "<script language='javascript' type='text/javascript'> alert('先选择文件')</sc

.Net常用技巧_将DataGridView的内容转换成DataTable

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using Utility; namespace MyTool { public class GetDgvToT

带复杂表头合并单元格的HtmlTable转换成DataTable并导出Excel(转)

步骤: 一.前台JS取HtmlTable数据,根据设定的分隔符把数据拼接起来 <!--导出Excel--> <script type="text/javascript"> //导出Excel function exportExcel() { var data = ""; $("#divRptTable").find("table").find("tr").each(function

将泛类型集合List类转换成DataTable

/// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list">泛类型集合</param> /// <returns></returns> public static DataTable ListToDataTable<T>(List<T> entitys) { //检查实体集合不能为空 if (

讲List转换成DataTable

/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list"></param> /// <returns></returns> public static System.Data.DataTable ListToDataTable(IList list) { System.Data.DataTable result = new

C#_List转换成DataTable

/// <summary> /// 讲list集合转换成datatable /// </summary> /// <param name="list"></param> /// <returns></returns> public static System.Data.DataTable ListToDataTable(IList list) { System.Data.DataTable result = new