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
