Asp.net static method Grid to DataTable method implementation steps
- 2020-06-01 09:32:56
- OfStack
After the GridView binding of DataTable, how to get the value displayed after the GridView binding? Under the background of the project requirements, the method to get the cell display text was searched, and then a static method was written. After the use of bug in the project and the repair of bug, it was relatively stable.
Only le le than all le le, put up the code for everyone to correct.
Only le le than all le le, put up the code for everyone to correct.
#region ================GridView turn DataTable methods ================
/// <summary>GridView turn DataTable Copyright: domain of knowledge http://www.qqextra.com . http://blog.csdn.net/ls_man Please quote the source </summary>
/// <param name="gv"> Of the bound data source GridView</param>
/// <param name="showHideColumn"> Whether to display hidden columns </param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, Boolean showHideColumn)
{
// The processed data table
DataTable dt = new DataTable();
// Records meet the criteria index
int[] columnIndexs = new int[gv.HeaderRow.Cells.Count];
// Recording indicator from 0 start
int columnIndexsCount = 0;
// Initialize the dt The column name
for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
{
// To get the column name
string columnName = GetCellText(gv.HeaderRow.Cells[i]);
//string columnName = gv.HeaderRow.Cells[i].Text;
// The column is not empty // And visible
if (!string.IsNullOrEmpty(columnName))
{
// Whether to display hidden columns
if (gv.HeaderRow.Cells[i].Visible || showHideColumn)
{
// Column names are not allowed to be repeated
if (!dt.Columns.Contains(columnName))
{
//dt In the new 1 column
DataColumn dc = dt.Columns.Add();
// The column name
dc.ColumnName = columnName;
// The type of data stored
dc.DataType = typeof(string);
// Record qualified column indexes
columnIndexs[columnIndexsCount] = i;
// Recording indicator +1
columnIndexsCount++;
}
}
}
}// Copyright: domain of knowledge http://www.qqextra.com . http://blog.csdn.net/ls_man Please quote the source
//GridView Copying rows into an array is easy to manipulate
GridViewRow[] allGridViewRow = new GridViewRow[gv.Rows.Count];
gv.Rows.CopyTo(allGridViewRow, 0);
// Add data to dt In the
foreach (GridViewRow row in allGridViewRow)
{
// create 1 line
DataRow dr = dt.NewRow();
// Qualified columns
for (int i = 0; i < columnIndexsCount; i++)
{
// Gets the display text and saves it
dr[i] = GetCellText(row.Cells[columnIndexs[i]]);
}
//dt Middle add row
dt.Rows.Add(dr);
}
// Returns the processed data
return dt;
}
/// <summary>GridView turn DataTable Copyright: domain of knowledge http://www.qqextra.com . http://blog.csdn.net/ls_man Please quote the source </summary>
/// <param name="gv"> Not bound to the data source GridView</param>
/// <param name="dtSource">GridView The data source </param>
/// <param name="showHideColumn"> Whether to display hidden columns </param>
/// <returns>DataTable</returns>
public static DataTable GridViewToDataTable(GridView gv, DataTable dtSource, Boolean showHideColumn)
{
// Bind the raw data to GridView
gv.DataSource = dtSource;
gv.DataBind();
// Set to no paging
gv.AllowPaging = false;<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">// Copyright: domain of knowledge http://www.qqextra.com . http://blog.csdn.net/ls_man Please quote the source
//GridView turn DataTable And return
return GridViewToDataTable(gv, showHideColumn);
}
#endregion
#region ================ Private tool method ================
/// <summary> To obtain TableCell Display text of Copyright: domain of knowledge http://www.qqextra.com . http://blog.csdn.net/ls_man Please quote the source </summary>
/// <param name="cell">TableCell</param>
/// <returns>string</returns>
private static string GetCellText(TableCell cell)
{
string cellText = cell.Text;
// Normal text (no controls) is returned directly
if (!string.IsNullOrEmpty(cellText))
{
// Return display text
return cellText.Replace(" ", "");
}
// traverse cell In the control
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;
} Copyright: domain of knowledge http://www.qqextra.com . http://blog.csdn.net/ls_man Please quote the source
if (control != null && control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
if (lc != null)
{
// Jump out to the 1 step foreach
continue;
}
ITextControl l = control as ITextControl;
cellText += l.Text.Replace("\r\n", "").Trim();
continue;
}
}
// Return display text
return cellText;
}
#endregion
</SPAN>