国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Asp.net靜態(tài)方法之Grid轉DataTable方法實現(xiàn)步驟

2024-07-10 13:16:48
字體:
來源:轉載
供稿:網友
GridView綁定DataTable后,如何獲取GridView綁定后顯示的值,在項目需求需要的背景下,搜索了獲取單元格顯示文本的方法,然后寫了一個靜態(tài)方法,經過在項目中的使用,bug的修復,較為穩(wěn)定。

獨樂樂不如眾樂樂,把代碼貼出來供大家指正。

復制代碼 代碼如下:


#region ================GridView轉DataTable方法================
/// <summary>GridView轉DataTable 版權:求知域,轉載請注明出處</summary>
/// <param>已綁定數據源的GridView</param>
/// <param>是否顯示隱藏列</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++;
}
}
}
}//版權:求知域,轉載請注明出處
//GridView行復制到數組中便于操作
GridViewRow[] allGridViewRow = new GridViewRow[gv.Rows.Count];
gv.Rows.CopyTo(allGridViewRow, 0);
//數據添加到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);
}
//返回處理后的數據
return dt;
}
/// <summary>GridView轉DataTable 版權:求知域,轉載請注明出處</summary>
/// <param>未綁定數據源的GridView</param>
/// <param>GridView的數據源</param>
/// <param>是否顯示隱藏列</param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, DataTable dtSource, Boolean showHideColumn)
{
//綁定原始數據到GridView
gv.DataSource = dtSource;
gv.DataBind();
//設置為不分頁
gv.AllowPaging = false;<SPAN>//版權:求知域,轉載請注明出處
//GridView轉DataTable并返回
return GridViewToDataTable(gv, showHideColumn);
}
#endregion
#region ================私有工具方法================
/// <summary>獲取TableCell的顯示文本 版權:求知域,轉載請注明出處</summary>
/// <param>TableCell</param>
/// <returns>string</returns>
private static string GetCellText(TableCell cell)
{
string cellText = cell.Text;
//常規(guī)文本(無控件)直接返回
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
</SPAN>

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 梅州市| 平塘县| 方山县| 安康市| 通榆县| 杭州市| 崇州市| 昌吉市| 嘉黎县| 喜德县| 鄱阳县| 江孜县| 嘉峪关市| 宜川县| 新疆| 浦县| 德江县| 饶平县| 河池市| 桃江县| 石柱| 库尔勒市| 佛教| 罗山县| SHOW| 扎兰屯市| 左云县| 通山县| 博白县| 柳州市| 龙门县| 寿光市| 开平市| 民乐县| 天峻县| 呼伦贝尔市| 大港区| 合肥市| 鄯善县| 浙江省| 仁布县|