asp.net static method of Grid-DataTable method Implementation steps _ Practical skills

Source: Internet
Author: User
After the GridView binds the DataTable, how to obtain the value displayed after the GridView binding, searches for the method to get the cell display text in the context of the requirements of the project, and then writes a static method, which is used in the project and the bug is fixed more stably.

Alone Lele as the public Lele, the code posted out for everyone to correct me.
Copy Code code as follows:

The method of #region ================gridview to ================
<summary>gridview to the DataTable Copyright: Knowledge Domain Http://www.qqextra.com,http://blog.csdn.net/ls_man reproduced please indicate the source </ Summary>
<param name= "GV" > Bound data Source gridview</param>
<param name= "Showhidecolumn" > Show hidden Columns </param>
<returns>DataTable</returns>
public static DataTable gridviewtodatatable (GridView GV, Boolean Showhidecolumn)
{
Data table after processing
DataTable dt = new DataTable ();
Record Criteria Index
int[] Columnindexs = new INT[GV. HeaderRow.Cells.Count];
Record indicator starting from 0
int columnindexscount = 0;
Initializing a DT column name
for (int i = 0; i < GV. HeaderRow.Cells.Count; i++)
{
Get Column Name
String columnName = Getcelltext (GV. Headerrow.cells[i]);
String columnName = GV. Headerrow.cells[i]. Text;
Column name not empty//visible
if (!string. IsNullOrEmpty (ColumnName))
{
Whether to show hidden columns
if (GV). Headerrow.cells[i]. Visible | | Showhidecolumn)
{
Column name does not allow duplicate
if (!dt. Columns.contains (ColumnName))
{
A new column in DT
DataColumn DC = dt. Columns.Add ();
Column Name
dc. ColumnName = ColumnName;
Types of data stored
dc. DataType = typeof (String);
To record a qualifying column index
Columnindexs[columnindexscount] = i;
Record indicator +1
columnindexscount++;
}
}
}
}//Copyright: Knowledge domain Http://www.qqextra.com,http://blog.csdn.net/ls_man reprint please specify the source
The GridView line is copied into the array for easy operation
gridviewrow[] Allgridviewrow = new GRIDVIEWROW[GV. Rows.Count];
Gv. Rows.copyto (allgridviewrow, 0);
Data added to DT
foreach (GridViewRow row in Allgridviewrow)
{
Create a row
DataRow dr = dt. NewRow ();
Eligible columns
for (int i = 0; i < Columnindexscount; i++)
{
Get display text and save
Dr[i] = Getcelltext (row. Cells[columnindexs[i]]);
}
DT To add this row
Dt. Rows.Add (DR);
}
Returns the processed data
return DT;
}
<summary>gridview to the DataTable Copyright: Knowledge Domain Http://www.qqextra.com,http://blog.csdn.net/ls_man reproduced please indicate the source </ Summary>
<param name= "GV" > Unbound data source gridview</param>
<param name= "Dtsource" >gridview data source </param>
<param name= "Showhidecolumn" > Show hidden Columns </param>
<returns>DataTable</returns>
public static DataTable gridviewtodatatable (GridView GV, DataTable Dtsource, Boolean Showhidecolumn)
{
Binding raw data to the GridView
Gv. DataSource = Dtsource;
Gv. DataBind ();
Set to No 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 Reprint please indicate the source
The GridView turns the DataTable and returns
Return gridviewtodatatable (GV, Showhidecolumn);
}
#endregion
#region ================ Private Tool Method ================
<summary> get TableCell display text copyright: Knowledge Domain Http://www.qqextra.com,http://blog.csdn.net/ls_man reproduced please indicate the source </summary >
<param name= "Cell" >TableCell</param>
<returns>string</returns>
private static string Getcelltext (TableCell cell)
{
String celltext = cell. Text;
Regular text (no controls) returns directly
if (!string. IsNullOrEmpty (Celltext))
{
return display text
Return Celltext.replace ("", "");
}
Traversing the controls in the cell
foreach 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 reproduced please indicate the source
if (Control!= null && control is Itextcontrol)
{
LiteralControl LC = control as LiteralControl;
if (LC!= NULL)
{
Jump to next foreach
Continue
}
Itextcontrol L = control as Itextcontrol;
Celltext + = L.text.replace ("\ r \ n", ""). Trim ();
Continue
}
}
return display text
return celltext;
}
#endregion
</SPAN>
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.