GridView轉化爲DataTable

/// <summary> 
/// 將 GridView 的數據生成 DataTable 
/// </summary> 
/// <param name="gv">GridView 對象</param> 
public DataTable GridViewToDataTable(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)
{
string columnName = cols[i];
if (String.IsNullOrEmpty(columnName))
{
continue;
}
if (table.Columns.Contains(columnName))
{
continue;
}
DataColumn dc = table.Columns.Add();
dc.ColumnName = columnName;
dc.DataType = typeof(string);
}
if (text == "&nbsp;")
{
text = "";
}
row[j] = text;
j++;
}
}
rowIndex++;
table.Rows.Add(row);
}
}
return table;
}
/// <summary>
/// 獲取單元格中的值
/// </summary>
/// <param name="cell"></param>
/// <returns></returns>
public 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 IButtonControl)
{
IButtonControl btn = control as IButtonControl;
text = btn.Text.Replace("/r/n", "").Trim();
break;
}
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;
}
相關文章
相關標籤/搜索