?? gridviewhelper.cs
字號:
/*----------------------------------------------------------------
// Copyright (C) 2007 螞蟻互動 版權所有。
//
// 文件名:GridView.cs
// 文件功能描述:
//
// 創建標識:jillzhang
// 修改標識:更新請訪問 http://jillzhang.cnblogs.com
// 修改描述:
//
// 修改標識:
// 修改描述:
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI;
namespace jzlib.Common
{
public class GridViewHelper
{
public static string GetCellText(TableCell cell)
{
string text = cell.Text;
if (!string.IsNullOrEmpty(text))
{
return text;
}
foreach (Control control in cell.Controls)
{
if (control != null && control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
if (lc != null)
{
continue;
}
ITextControl l = control as ITextControl;
text = l.Text.Replace("\r\n", "").Trim();
break;
}
}
return text;
}
/// <summary>
/// 從GridView的數據生成DataTable
/// </summary>
/// <param name="gv">GridView對象</param>
public static DataTable GridView2DataTable(GridView gv)
{
DataTable table = new DataTable();
int rowIndex = 0;
List<string> cols = new List<string>();
if (!gv.ShowHeader && gv.Columns.Count == 0)
{
return table;
}
GridViewRow headerRow = gv.HeaderRow;
int columnCount =headerRow.Cells.Count;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(headerRow.Cells[i]);
cols.Add(text);
}
foreach (GridViewRow r in gv.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
DataRow row = table.NewRow();
int j = 0;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(r.Cells[i]);
if (!String.IsNullOrEmpty(text))
{
if (rowIndex == 0)
{
DataColumn dc = table.Columns.Add();
string columnName = cols[i];
if (String.IsNullOrEmpty(columnName))
{
columnName = gv.Columns[i].HeaderText;
if (string.IsNullOrEmpty(columnName))
{
continue;
}
}
dc.ColumnName = columnName;
dc.DataType = typeof(string);
}
row[j] = text;
}
j++;
}
rowIndex++;
table.Rows.Add(row);
}
}
return table;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -