Asp.net靜態(tài)方法之Grid轉(zhuǎn)DataTable方法實(shí)現(xiàn)步驟
更新時(shí)間:2013年04月07日 16:05:16 作者:
GridView綁定DataTable后,如何獲取GridView綁定后顯示的值,在項(xiàng)目需求的背景下寫了一個(gè)靜態(tài)方法,經(jīng)過(guò)在項(xiàng)目中的使用,bug的修復(fù),較為穩(wěn)定
GridView綁定DataTable后,如何獲取GridView綁定后顯示的值,在項(xiàng)目需求需要的背景下,搜索了獲取單元格顯示文本的方法,然后寫了一個(gè)靜態(tài)方法,經(jīng)過(guò)在項(xiàng)目中的使用,bug的修復(fù),較為穩(wěn)定。
獨(dú)樂(lè)樂(lè)不如眾樂(lè)樂(lè),把代碼貼出來(lái)供大家指正。
#region ================GridView轉(zhuǎn)DataTable方法================
/// <summary>GridView轉(zhuǎn)DataTable 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處</summary>
/// <param name="gv">已綁定數(shù)據(jù)源的GridView</param>
/// <param name="showHideColumn">是否顯示隱藏列</param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, Boolean showHideColumn)
{
//處理后的數(shù)據(jù)表
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)
{
//列名不允許重復(fù)
if (!dt.Columns.Contains(columnName))
{
//dt中新增一列
DataColumn dc = dt.Columns.Add();
//列名
dc.ColumnName = columnName;
//存儲(chǔ)的數(shù)據(jù)類型
dc.DataType = typeof(string);
//記錄符合條件的列索引
columnIndexs[columnIndexsCount] = i;
//記錄指示器+1
columnIndexsCount++;
}
}
}
}//版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
//GridView行復(fù)制到數(shù)組中便于操作
GridViewRow[] allGridViewRow = new GridViewRow[gv.Rows.Count];
gv.Rows.CopyTo(allGridViewRow, 0);
//數(shù)據(jù)添加到dt中
foreach (GridViewRow row in allGridViewRow)
{
//創(chuàng)建一行
DataRow dr = dt.NewRow();
//符合條件的列
for (int i = 0; i < columnIndexsCount; i++)
{
//獲取顯示文本并保存
dr[i] = GetCellText(row.Cells[columnIndexs[i]]);
}
//dt中增加此行
dt.Rows.Add(dr);
}
//返回處理后的數(shù)據(jù)
return dt;
}
/// <summary>GridView轉(zhuǎn)DataTable 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處</summary>
/// <param name="gv">未綁定數(shù)據(jù)源的GridView</param>
/// <param name="dtSource">GridView的數(shù)據(jù)源</param>
/// <param name="showHideColumn">是否顯示隱藏列</param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, DataTable dtSource, Boolean showHideColumn)
{
//綁定原始數(shù)據(jù)到GridView
gv.DataSource = dtSource;
gv.DataBind();
//設(shè)置為不分頁(yè)
gv.AllowPaging = false;<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">//版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
//GridView轉(zhuǎn)DataTable并返回
return GridViewToDataTable(gv, showHideColumn);
}
#endregion
#region ================私有工具方法================
/// <summary>獲取TableCell的顯示文本 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處</summary>
/// <param name="cell">TableCell</param>
/// <returns>string</returns>
private static string GetCellText(TableCell cell)
{
string cellText = cell.Text;
//常規(guī)文本(無(wú)控件)直接返回
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;
}版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
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
</SPAN>
獨(dú)樂(lè)樂(lè)不如眾樂(lè)樂(lè),把代碼貼出來(lái)供大家指正。
復(fù)制代碼 代碼如下:
#region ================GridView轉(zhuǎn)DataTable方法================
/// <summary>GridView轉(zhuǎn)DataTable 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處</summary>
/// <param name="gv">已綁定數(shù)據(jù)源的GridView</param>
/// <param name="showHideColumn">是否顯示隱藏列</param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, Boolean showHideColumn)
{
//處理后的數(shù)據(jù)表
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)
{
//列名不允許重復(fù)
if (!dt.Columns.Contains(columnName))
{
//dt中新增一列
DataColumn dc = dt.Columns.Add();
//列名
dc.ColumnName = columnName;
//存儲(chǔ)的數(shù)據(jù)類型
dc.DataType = typeof(string);
//記錄符合條件的列索引
columnIndexs[columnIndexsCount] = i;
//記錄指示器+1
columnIndexsCount++;
}
}
}
}//版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
//GridView行復(fù)制到數(shù)組中便于操作
GridViewRow[] allGridViewRow = new GridViewRow[gv.Rows.Count];
gv.Rows.CopyTo(allGridViewRow, 0);
//數(shù)據(jù)添加到dt中
foreach (GridViewRow row in allGridViewRow)
{
//創(chuàng)建一行
DataRow dr = dt.NewRow();
//符合條件的列
for (int i = 0; i < columnIndexsCount; i++)
{
//獲取顯示文本并保存
dr[i] = GetCellText(row.Cells[columnIndexs[i]]);
}
//dt中增加此行
dt.Rows.Add(dr);
}
//返回處理后的數(shù)據(jù)
return dt;
}
/// <summary>GridView轉(zhuǎn)DataTable 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處</summary>
/// <param name="gv">未綁定數(shù)據(jù)源的GridView</param>
/// <param name="dtSource">GridView的數(shù)據(jù)源</param>
/// <param name="showHideColumn">是否顯示隱藏列</param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, DataTable dtSource, Boolean showHideColumn)
{
//綁定原始數(shù)據(jù)到GridView
gv.DataSource = dtSource;
gv.DataBind();
//設(shè)置為不分頁(yè)
gv.AllowPaging = false;<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">//版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
//GridView轉(zhuǎn)DataTable并返回
return GridViewToDataTable(gv, showHideColumn);
}
#endregion
#region ================私有工具方法================
/// <summary>獲取TableCell的顯示文本 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處</summary>
/// <param name="cell">TableCell</param>
/// <returns>string</returns>
private static string GetCellText(TableCell cell)
{
string cellText = cell.Text;
//常規(guī)文本(無(wú)控件)直接返回
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;
}版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
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
</SPAN>
您可能感興趣的文章:
- asp.net實(shí)現(xiàn)導(dǎo)出DataTable數(shù)據(jù)到Word或者Excel的方法
- asp.net實(shí)現(xiàn)數(shù)據(jù)從DataTable導(dǎo)入到Excel文件并創(chuàng)建表的方法
- Asp.net中DataTable導(dǎo)出到Excel的方法介紹
- asp.net 讀取Excel數(shù)據(jù)到DataTable的代碼
- ASP.NET DataTable去掉重復(fù)行的2種方法
- ASP.NET中DataTable與DataSet之間的轉(zhuǎn)換示例
- ASP.NET怎么操作DataTable實(shí)例應(yīng)用
- Asp.net下使用Jquery Ajax傳送和接收DataTable的代碼
- asp.net 數(shù)據(jù)庫(kù)的連接和datatable類
- Asp.net實(shí)現(xiàn)選擇性的保留DataTable中的列
- asp.net DataTable導(dǎo)出Excel自定義列名的方法
相關(guān)文章
asp.net操作Word實(shí)現(xiàn)批量替換
這篇文章主要介紹了asp.net操作Word實(shí)現(xiàn)批量替換的方法,需要的朋友可以參考下2015-10-10
SignalR Self Host+MVC等多端消息推送服務(wù)(二)
這篇文章主要為大家詳細(xì)介紹了SignalR Self Host+MVC等多端消息推送服務(wù)的第二篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
ASP.NET中Session和Cache的區(qū)別總結(jié)
這篇文章主要介紹了ASP.NET中Session和Cache的區(qū)別總結(jié),本文結(jié)合使用經(jīng)驗(yàn),總結(jié)出了5點(diǎn)Session緩存和Cache緩存的區(qū)別,需要的朋友可以參考下2015-06-06
在ASP.NET中用存儲(chǔ)過(guò)程執(zhí)行SQL語(yǔ)句
存儲(chǔ)過(guò)程:是一組為了完成特定功能的SQL語(yǔ)句集,經(jīng)編譯后存儲(chǔ)在數(shù)據(jù)庫(kù)中。用戶通過(guò)指定存儲(chǔ)過(guò)程的名字并給出參數(shù)(如果該存儲(chǔ)過(guò)程帶有參數(shù))來(lái)執(zhí)行它。存儲(chǔ)過(guò)程是數(shù)據(jù)庫(kù)中的一個(gè)重要對(duì)象,任何一個(gè)設(shè)計(jì)良好的數(shù)據(jù)庫(kù)應(yīng)用程序都應(yīng)該用到存儲(chǔ)過(guò)程。2010-04-04
asp.net 驗(yàn)證碼的簡(jiǎn)單制作(vb.net+C#)
asp.net中實(shí)現(xiàn)簡(jiǎn)單驗(yàn)證碼的方法,需要的朋友可以參考下2012-05-05
.Net Core + Nginx實(shí)現(xiàn)項(xiàng)目負(fù)載均衡的全步驟
這篇文章主要給大家介紹了關(guān)于.Net Core + Nginx實(shí)現(xiàn)項(xiàng)目負(fù)載均衡的相關(guān)資料,文中通過(guò)示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
asp.net中強(qiáng)制取消TFS2008中其它成員的簽出文件的方法
有個(gè)項(xiàng)目,以前的成員離職了,剛好又簽出了一個(gè)文件在TFS中并且上了鎖,導(dǎo)致后面的維護(hù)無(wú)法簽入和生成。在網(wǎng)上查了一下,找到了如下解決辦法2012-08-08

