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