当前位置:网站首页>将GrilView中的数据转换成DataTable
将GrilView中的数据转换成DataTable
2022-07-28 05:23:00 【海乐学习】
//在webform开发中难免会遇到将GrilView中的数据转换成DataTable,下面的类将实现这个功能,但仅是显示出来的数据,如有分页得另行处理。
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI;
public class GridViewHelper
{
//从GridView的数据生成DataTable
public static DataTable GridViewToDataTable(GridView gv)
{
DataTable table = new DataTable();
int rowIndex = 0;
List<string> cols = new List<string>();
if (!gv.ShowHeader && gv.Columns.Count == 0)
{
return table;
}
GridViewRow headerRow = gv.HeaderRow;
int columnCount =headerRow.Cells.Count;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(headerRow.Cells[i]);
cols.Add(text);
}
foreach (GridViewRow r in gv.Rows)
{
if (r.RowType == DataControlRowType.DataRow)
{
DataRow row = table.NewRow();
int j = 0;
for (int i = 0; i < columnCount; i++)
{
string text = GetCellText(r.Cells[i]);
if (!String.IsNullOrEmpty(text))
{
if (rowIndex == 0)
{
DataColumn dc = table.Columns.Add();
string columnName = cols[i];
if (String.IsNullOrEmpty(columnName))
{
columnName = gv.Columns[i].HeaderText;
if (string.IsNullOrEmpty(columnName))
{
continue;
}
}
dc.ColumnName = columnName;
dc.DataType = typeof(string);
}
row[j] = text;
}
j++;
}
rowIndex++;
table.Rows.Add(row);
}
}
return table;
}
public static string GetCellText(TableCell cell)
{
string text = cell.Text;
if (!string.IsNullOrEmpty(text))
{
return text;
}
foreach (Control control in cell.Controls)
{
if (control != null && control is ITextControl)
{
LiteralControl lc = control as LiteralControl;
if (lc != null)
{
continue;
}
ITextControl l = control as ITextControl;
text = l.Text.Replace("\r\n", "").Trim();
break;
}
}
return text;
}
}边栏推荐
- tf.keras搭建神经网络功能扩展
- 详解爬电距离和电气间隙
- How to improve the efficiency of small program development?
- Overview of unconstrained low resolution face recognition II: heterogeneous low resolution face recognition methods
- 5、 Video processing and GStreamer
- ESXi 社区版网卡驱动
- Ssh/scp breakpoint resume Rsync
- Deep learning (self supervised: Moco V3): An Empirical Study of training self supervised vision transformers
- Prime_ Series range from detection to weight lifting
- Scenario solution of distributed cluster architecture: cluster clock synchronization
猜你喜欢

Nanjing University of Posts and Telecommunications CTF topic writeup (II) including topic address

生活随机-1

Scenario solution of distributed cluster architecture: cluster clock synchronization

EMC实验实战案例-ESD静电实验

Deep learning (self supervision: Moco V2) -- improved bases with momentum contractual learning

基于tensorflow搭建神经网络

基于差值扩展的可逆水印方法

Geek challenge 2019-sql injection five questions PW

Deep learning pay attention to MLPs

Deep learning (incremental learning) - (iccv) striking a balance between stability and plasticity for class incremental learning
随机推荐
机群作业管理系统,求解答进程方面的疑问
关于Fusion on Apple Silicon的谨慎猜测
What is the process of building a small program?
Clustering of machine learning
Prime_ Series range from detection to weight lifting
Automatic scheduled backup of remote MySQL scripts
深度学习——Patches Are All You Need
Summary of command injection bypass methods
What is the detail of the applet development process?
Small program development solves the anxiety of retail industry
ESXi社区版网卡驱动再次更新
Classification of iris based on Neural Network
Why is the kotlin language not popular now? What's your opinion?
深度学习(自监督:MoCo v2)——Improved Baselines with Momentum Contrastive Learning
Protecting Against DNN Model Stealing Attacks 论文阅读心得
Realization of topic function
Which is more reliable for small program development?
Deep learning pay attention to MLPs
Latex入门
深度学习(自监督:MoCo V3):An Empirical Study of Training Self-Supervised Vision Transformers