Asp.net static method implementation steps of Grid-to-DataTable Method

Source: Internet
Author: User

After the GridView is bound to a able, how does one obtain the value displayed after the GridView is bound? In the context required by the project, search for the method to retrieve the text displayed by cells, and then write a static method, after use in the project, the bug is fixed and relatively stable.

Lele is better than zlele, And you can post the code for correction.Copy codeThe Code is as follows: # region ======================== the GridView able method of the GridView to able ======================
/// <Summary> GridView to able copyright: knowledge domain http://www.qqextra.com, http://blog.csdn.net/ls_man source </summary>
/// <Param name = "gv"> the GridView bound to the data source </param>
/// <Param name = "showHideColumn"> show hidden columns </param>
/// <Returns> DataTable </returns>
Public static DataTable GridViewToDataTable (GridView gv, Boolean showHideColumn)
{
// Processed data table
DataTable dt = new DataTable ();
// Record the qualified Index
Int [] columnIndexs = new int [gv. HeaderRow. Cells. Count];
// Record indicator starts from 0
Int columnIndexsCount = 0;
// Initialize the dt column name
For (int I = 0; I <gv. HeaderRow. Cells. Count; I ++)
{
// Obtain the column name
String columnName = GetCellText (gv. HeaderRow. Cells [I]);
// String columnName = gv. HeaderRow. Cells [I]. Text;
// The column name is not empty // and visible
If (! String. IsNullOrEmpty (columnName ))
{
// Whether to show hidden Columns
If (gv. HeaderRow. Cells [I]. Visible | showHideColumn)
{
// Duplicate column names are not allowed
If (! Dt. Columns. Contains (columnName ))
{
// Add a new column in dt
DataColumn dc = dt. Columns. Add ();
// Column name
Dc. ColumnName = columnName;
// Data type stored
Dc. DataType = typeof (string );
// Record the column indexes that meet the criteria
ColumnIndexs [columnIndexsCount] = I;
// Record indicator + 1
ColumnIndexsCount ++;
}
}
}
} // Copyright: knowledge domain http://www.qqextra.com, http://blog.csdn.net/ls_man
// Copy rows from the GridView to the array for easy operation
GridViewRow [] allGridViewRow = new GridViewRow [gv. Rows. Count];
Gv. Rows. CopyTo (allGridViewRow, 0 );
// Add data to dt
Foreach (GridViewRow row in allGridViewRow)
{
// Create a row
DataRow dr = dt. NewRow ();
// Qualified Columns
For (int I = 0; I <columnIndexsCount; I ++)
{
// Obtain and save the displayed text
Dr [I] = GetCellText (row. Cells [columnIndexs [I]);
}
// Add this row in dt
Dt. Rows. Add (dr );
}
// Return the processed data
Return dt;
}
/// <Summary> GridView to able copyright: knowledge domain http://www.qqextra.com, http://blog.csdn.net/ls_man source </summary>
/// <Param name = "gv"> the GridView that is not bound to the data source </param>
/// <Param name = "dtSource"> data source of the GridView </param>
/// <Param name = "showHideColumn"> show hidden columns </param>
/// <Returns> DataTable </returns>
Public static DataTable GridViewToDataTable (GridView gv, DataTable dtSource, Boolean showHideColumn)
{
// Bind the original data to the GridView
Gv. DataSource = dtSource;
Gv. DataBind ();
// Set to not paging
Gv. AllowPaging = false; <SPAN style = "FONT-FAMILY: Arial, Helvetica, sans-serif"> // copyright: domain http://www.qqextra.com, http://blog.csdn.net/ls_man
// Convert the GridView to DataTable and return
Return GridViewToDataTable (gv, showHideColumn );
}
# Endregion
# Region ====================== private tool method ==============================
/// <Summary> get the display text of TableCell copyright: knowledge domain http://www.qqextra.com, http://blog.csdn.net/ls_man </summary>
/// <Param name = "cell"> TableCell </param>
/// <Returns> string </returns>
Private static string GetCellText (TableCell cell)
{
String cellText = cell. Text;
// Regular text (no control) is returned directly
If (! String. IsNullOrEmpty (cellText ))
{
// Return the displayed text
Return cellText. Replace ("","");
}
// Traverse the control in the 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;
} Copyright: knowledge domain http://www.qqextra.com, http://blog.csdn.net/ls_man
If (control! = Null & control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
If (lc! = Null)
{
// Jump out to the next step foreach
Continue;
}
ITextControl l = control as ITextControl;
CellText + = l. Text. Replace ("\ r \ n", ""). Trim ();
Continue;
}
}
// Return the displayed text
Return cellText;
}
# Endregion
</SPAN>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.