当前位置:网站首页>将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;
}
}边栏推荐
- 一、AMD - OpenVINO环境配置
- Realization of topic function
- 无约束低分辨率人脸识别综述二:异构低分辨率人脸识别方法
- Using neural network to predict the weather
- 无约束低分辨率人脸识别综述三:同质低分辨率人脸识别方法
- Knowledge point 21 generic
- Overview of unconstrained low resolution face recognition I: data sets for low resolution face recognition
- Which is more reliable for small program development?
- 深度学习(增量学习)——ICCV2021:SS-IL: Separated Softmax for Incremental Learning
- 利用辅助未标记数据增强无约束人脸识别《Boosting Unconstrained Face Recognition with Auxiliary Unlabeled Data》
猜你喜欢

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

How to improve the efficiency of small program development?

Internet of things interoperability system: classification, standards and future development

Reinforcement learning - incomplete observation problem, MCTs

Which is more reliable for small program development?

Which enterprises are suitable for small program production and small program development?

D2sc-gan: low resolution face recognition of classroom scenes based on dual depth and shallow channel generation confrontation network

Distributed cluster architecture scenario optimization solution: distributed scheduling problem

开关电源电路EMI设计在layout过程中注意事项

Reinforcement learning - proximal policy optimization algorithms
随机推荐
深度学习(增量学习)——ICCV2021:SS-IL: Separated Softmax for Incremental Learning
关于隔离电源断电瞬间MOSFET损坏问题分析
D2sc-gan: low resolution face recognition of classroom scenes based on dual depth and shallow channel generation confrontation network
Boosting unconstrained face recognition with auxiliary unlabeled data to enhance unconstrained face recognition
监控安装ESXi on Arm的树莓派4b的CPU温度
Reinforcement learning - proximal policy optimization algorithms
二、OpenVINO简述与构建流程
Reading experience of protecting against DNN model steaming attacks
ESXi on ARM v1.2 (2020年11月更新)
Geek challenge 2019-sql injection five questions PW
Deep learning (self supervised: Moco V3): An Empirical Study of training self supervised vision transformers
5、 Video processing and GStreamer
Deploy the project to GPU and run
The number of password errors during login is too many, and the user is blocked,
强化学习——策略学习
vscode uniapp
强化学习——Proximal Policy Optimization Algorithms
USB Network Native Driver for ESXi更新到支持ESXi7.0 Update 2
Interpreting the knowledge in a neural network
Building neural network based on tensorflow