Asp.net _ static method-convert a Grid to a able 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. [Csharp] # region ===================== the method for converting the GridView to DataTable ====================== /// <summary> the right to convert the GridView to the able: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man For more information, see </summary> /// <param name = "gv"> GridView bound to a data source </param> /// <param name = "showHideColumn"> whether show Hidden columns </param> /// <returns> DataTable </returns> public static DataTable GridViewToDataTable (GridView gv, boolean showHideColumn) {// the processed data table dt able dt = new DataTable (); // records the int [] columnIndexs = new int [gv. headerRow. cells. count]; // record indicator 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 the Hidden Column if (gv. HeaderRow. Cells [I]. Visible | showHideColumn) {// if (! Dt. columns. contains (columnName) {// Add a new column in dt DataColumn dc = dt. columns. add (); // column name dc. columnName = columnName; // the stored data type dc. dataType = typeof (string); // record columnIndexs [columnIndexsCount] = I; // record indicator + 1 columnIndexsCount ++ ;}}// copyright: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man Reprinted please specify the source // copy the row in the GridView to the array for easy operation of 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 column for (int I = 0; I <columnIndexsCount; I ++) {// obtain the displayed text and save dr [I] = GetCellText (row. cells [columnIndexs [I]);} // Add this line to dt. rows. add (dr) ;}// return the processed data return dt ;}/// <summary> GridView to DataTable copyright: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man For more information, see </summary> /// <param name = "gv"> GridView that is not bound to a data source </param> /// <param name = "dtSource"> 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 non-Paging gv. allowPaging = false; <SPAN style = "FONT-FAMILY: Arial, Helvetica, sans-serif"> // copyright: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man Reprinted, please specify the source // GridView to convert DataTable and return GridViewToDataTable (gv, showHideColumn );} # endregion # region ====================== private tool method ===========================/ // <summary> obtain the copyright of the displayed TableCell text: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man For more information, see </summary> /// <param name = "cell"> TableCell </param> /// <returns> string </returns> private static string GetCellText (tableCell cell) {string cellText = cell. text; // regular Text (no control) directly returns if (! String. isNullOrEmpty (cellText) {// return the displayed text return cellText. replace ("", "") ;}// traverses 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: 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 # region ====================== GridView able method ====================== /// <summary> the copyright for converting GridView to able: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man For more information, see </summary> /// <param name = "gv"> GridView bound to a data source </param> /// <param name = "showHideColumn"> whether show Hidden columns </param> /// <returns> DataTable </returns> public static DataTable GridViewToDataTable (GridView gv, boolean showHideColumn) {// the processed data table dt able dt = new DataTable (); // records the int [] columnIndexs = new int [gv. headerRow. cells. count]; // record indicator 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 the Hidden Column if (gv. HeaderRow. Cells [I]. Visible | showHideColumn) {// if (! Dt. columns. contains (columnName) {// Add a new column in dt DataColumn dc = dt. columns. add (); // column name dc. columnName = columnName; // the stored data type dc. dataType = typeof (string); // record columnIndexs [columnIndexsCount] = I; // record indicator + 1 columnIndexsCount ++ ;}}// copyright: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man Reprinted please specify the source // copy the row in the GridView to the array for easy operation of 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 column for (int I = 0; I <columnIndexsCount; I ++) {// obtain the displayed text and save dr [I] = GetCellText (row. cells [columnIndexs [I]);} // Add this line to dt. rows. add (dr) ;}// return the processed data return dt ;}/// <summary> GridView to DataTable copyright: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man For more information, see </summary> /// <param name = "gv"> GridView that is not bound to a data source </param> /// <param name = "dtSource"> 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 non-Paging gv. allowPaging = false; // copyright: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man Reprinted, please specify the source // GridView to convert DataTable and return GridViewToDataTable (gv, showHideColumn );} # endregion # region ====================== private tool method ===========================/ // <summary> obtain the copyright of the displayed TableCell text: knowledge domain http://www.qqextra.com , http://blog.csdn.net/ls_man For more information, see </summary> /// <param name = "cell"> TableCell </param> /// <returns> string </returns> private static string GetCellText (tableCell cell) {string cellText = cell. text; // regular Text (no control) directly returns if (! String. isNullOrEmpty (cellText) {// return the displayed text return cellText. replace ("", "") ;}// traverses the Control foreach (control Control in cell. controls) {www.2cto.com 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

Related Article

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.