日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Asp.net中怎么將Grid轉(zhuǎn)換為DataTable-創(chuàng)新互聯(lián)

Asp.net中怎么將Grid轉(zhuǎn)換為DataTable,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。

創(chuàng)新互聯(lián)公司是一家專注于網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站與策劃設(shè)計(jì),武侯網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:武侯等地區(qū)。武侯做網(wǎng)站價(jià)格咨詢:13518219792

 代碼如下:



#region ================GridView轉(zhuǎn)DataTable方法================
///

GridView轉(zhuǎn)DataTable 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
/// 已綁定數(shù)據(jù)源的GridView
/// 是否顯示隱藏列
/// DataTable
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;
}
/// GridView轉(zhuǎn)DataTable 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
/// 未綁定數(shù)據(jù)源的GridView
/// GridView的數(shù)據(jù)源
/// 是否顯示隱藏列
/// DataTable
public static DataTable GridViewToDataTable(GridView gv, DataTable dtSource, Boolean showHideColumn)
{
//綁定原始數(shù)據(jù)到GridView
gv.DataSource = dtSource;
gv.DataBind();
//設(shè)置為不分頁(yè)
gv.AllowPaging = false;//版權(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 ================私有工具方法================
/// 獲取TableCell的顯示文本 版權(quán):求知域http://www.qqextra.com,http://blog.csdn.net/ls_man轉(zhuǎn)載請(qǐng)注明出處
/// TableCell
/// string
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


看完上述內(nèi)容,你們掌握Asp.net中怎么將Grid轉(zhuǎn)換為DataTable的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!


網(wǎng)站欄目:Asp.net中怎么將Grid轉(zhuǎn)換為DataTable-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://www.dlmjj.cn/article/jgjhj.html