[英]Why conversion from Gridview to datatable fails?
我一直在嘗試將GRIDVIEW轉換為DataTable,但它不給我數據,它僅給我GridView的HEADERS,而不給我其中的數據。 為什么呢 我在gridview中使用模板字段
從事件中調用它:
DataTable dt = GetDataTable(GridView DenominationsWiseTransactions);
轉換功能:
DataTable GetDataTable(GridView dtg)
{
DataTable dt = new DataTable();
// add the columns to the datatable
if (dtg.HeaderRow != null)
{
for (int i = 0; i < dtg.HeaderRow.Cells.Count; i++)
{
dt.Columns.Add(dtg.HeaderRow.Cells[i].Text);
}
}
// add each of the data rows to the table
foreach (GridViewRow row in dtg.Rows)
{
DataRow dr;
dr = dt.NewRow();
for (int i = 0; i < row.Cells.Count; i++)
{
dr[i] = row.Cells[i].Text.Replace(" ", "");
}
dt.Rows.Add(dr);
}
return dt;
}
因此,當您遍歷數據網格中的行時,它將包括標題,行和頁腳。
要僅查看數據行,您需要執行以下操作:
if (e.Row.RowType == DataControlRowType.DataRow)
{ //convert that row }
轉換行可能需要做一些工作,但是至少現在您只需要關心數據行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.