Asp.net靜態方法之Grid轉DataTable方法實現步驟

來源:互聯網
上載者:User

GridView綁定DataTable後,如何擷取GridView綁定後顯示的值,在項目需求需要的背景下,搜尋了擷取儲存格顯示文本的方法,然後寫了一個靜態方法,經過在項目中的使用,bug的修複,較為穩定。

獨樂樂不如眾樂樂,把代碼貼出來供大家指正。 複製代碼 代碼如下:#region ================GridView轉DataTable方法================
/// <summary>GridView轉DataTable 著作權:求知域http://www.qqextra.com,http://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++;
}
}
}
}//著作權:求知域http://www.qqextra.com,http://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 著作權:求知域http://www.qqextra.com,http://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">//著作權:求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉載請註明出處
//GridView轉DataTable並返回
return GridViewToDataTable(gv, showHideColumn);
}
#endregion
#region ================私人工具方法================
/// <summary>擷取TableCell的顯示文本 著作權:求知域http://www.qqextra.com,http://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;
}著作權:求知域http://www.qqextra.com,http://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
</SPAN>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.