Asp.net_静态方法之Grid转DataTable方法分享
独乐乐不如众乐乐,把代码贴出来供大家指正。
[csharp]
#region ================gridview转datatable方法================
/// <summary>gridview转datatable 版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</summary>
/// <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 = 0;
//初始化dt列名
for (int i = 0; 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++;
}
}
}
}//版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处
//gridview行复制到数组中便于操作
gridviewrow[] allgridviewrow = new gridviewrow[gv.rows.count];
gv.rows.copyto(allgridviewrow, 0);
//数据添加到dt中
foreach (gridviewrow row in allgridviewrow)
{
//创建一行
datarow dr = dt.newrow();
//符合条件的列
for (int i = 0; i < columnindexscount; i++)
{
//获取显示文本并保存
dr[i] = getcelltext(row.cells[columnindexs[i]]);
}
//dt中增加此行
dt.rows.add(dr);
}
//返回处理后的数据
return dt;
}
/// <summary>gridview转datatable 版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</summary>
/// <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;<span style="font-family: arial, helvetica, sans-serif">//版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处
//gridview转datatable并返回
return gridviewtodatatable(gv, showhidecolumn);
}
#endregion
#region ================私有工具方法================
/// <summary>获取tablecell的显示文本 版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</summary>
/// <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;
}版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处
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 ================gridview转datatable方法================
/// <summary>gridview转datatable 版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</summary>
/// <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 = 0;
//初始化dt列名
for (int i = 0; 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++;
}
}
}
}//版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处
//gridview行复制到数组中便于操作
gridviewrow[] allgridviewrow = new gridviewrow[gv.rows.count];
gv.rows.copyto(allgridviewrow, 0);
//数据添加到dt中
foreach (gridviewrow row in allgridviewrow)
{
//创建一行
datarow dr = dt.newrow();
//符合条件的列
for (int i = 0; i < columnindexscount; i++)
{
//获取显示文本并保存
dr[i] = getcelltext(row.cells[columnindexs[i]]);
}
//dt中增加此行
dt.rows.add(dr);
}
//返回处理后的数据
return dt;
}
/// <summary>gridview转datatable 版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</summary>
/// <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;//版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处
//gridview转datatable并返回
return gridviewtodatatable(gv, showhidecolumn);
}
#endregion
#region ================私有工具方法================
/// <summary>获取tablecell的显示文本 版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处</summary>
/// <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)
{ www.2cto.com
if (control != null && control is ibuttoncontrol)
{
ibuttoncontrol btn = control as ibuttoncontrol;
celltext += btn.text.replace("\r\n", "").trim();
continue;
}版权:求知域https://www.qqextra.com,https://blog.csdn.net/ls_man转载请注明出处
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