GridView绑定DataTable后,如何获取GridView绑定后显示的值,在项目需求需要的背景下,搜索了获取单元格显示文本的方法,然后写了一个静态方法,经过在项目中的使用,bug的修复,较为稳定。
#region ================GridView转DataTable方法================
/// <param name="gv">已绑定数据源的GridView</param>
/// <param name="showHideColumn">是否显示隐藏列</param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, Boolean showHideColumn)
{
//处理后的数据表
DataTable dt = new DataTable(); //记录符合条件索引
int[] columnIndexs = new int[gv.HeaderRow.Cells.Count];
//记录指示器从0开始
int columnIndexsCount = ; //初始化dt列名
for (int i = ; 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++;
}
}
}
} //GridView行复制到数组中便于操作
GridViewRow[] allGridViewRow = new GridViewRow[gv.Rows.Count];
gv.Rows.CopyTo(allGridViewRow, ); //数据添加到dt中
foreach (GridViewRow row in allGridViewRow)
{
//创建一行
DataRow dr = dt.NewRow();
//符合条件的列
for (int i = ; i < columnIndexsCount; i++)
{
//获取显示文本并保存
dr[i] = GetCellText(row.Cells[columnIndexs[i]]);
}
//dt中增加此行
dt.Rows.Add(dr);
}
//返回处理后的数据
return dt;
} /// <param name="gv">未绑定数据源的GridView</param>
/// <param name="dtSource">GridView的数据源</param>
/// <param name="showHideColumn">是否显示隐藏列</param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, DataTable dtSource, Boolean showHideColumn)
{
//绑定原始数据到GridView
gv.DataSource = dtSource;
gv.DataBind();
//设置为不分页
gv.AllowPaging = false;
//GridView转DataTable并返回
return GridViewToDataTable(gv, showHideColumn);
}
#endregion #region ================私有工具方法================
/// <param name="cell">TableCell</param>
/// <returns>string</returns>
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("\r\n", "").Trim();
continue;
}
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("\r\n", "").Trim();
continue;
}
}
//返回显示文本
return cellText;
}
#endregion
#region ================另一种方法================
public static DataTable GetGridDataTable(GridView grid)
{
DataTable dt = new DataTable();
DataColumn dc;//创建列
DataRow dr; //创建行
//构造列
for (int i = ; i < grid.Columns.Count; i++)
{
dc = new DataColumn();
dc.ColumnName = grid.Columns[i].HeaderText;
dt.Columns.Add(dc);
}
//构造行
for (int i = ; i < grid.Rows.Count; i++)
{
dr = dt.NewRow();
for (int j = ; j < grid.Columns.Count; j++)
{
dr[j] = grid.Rows[i].Cells[j].Text;
}
dt.Rows.Add(dr);
} return dt;
}
#endregion