当前位置:网站首页>Convert data in grilview into datatable
Convert data in grilview into datatable
2022-07-28 06:20:00 【Haile learning】
// stay webform It is inevitable to encounter problems in development GrilView Convert data in to DataTable, The following classes will implement this function , But only the displayed data , If there is paging, it must be handled separately .
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI;
public class GridViewHelper
{
// from GridView Data generation 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;
}
}边栏推荐
- ESXi社区版网卡驱动再次更新
- Overview of unconstrained low resolution face recognition I: data sets for low resolution face recognition
- 弹出消息对话框的方法
- 如何测试工业以太网线缆(利用FLUKE DSX-8000)?
- (PHP graduation project) obtained based on PHP novel website management system
- What is the process of building a small program?
- 关于隔离电源断电瞬间MOSFET损坏问题分析
- AEM-TESTpro K50和南粤勘察结下的缘分
- 深度学习数据窃取攻击在数据沙箱模式下的威胁分析与防御方法研究阅读心得
- Web滚动字幕(MARQUEE示例)
猜你喜欢

Tornado first met

Differences between processes and threads

(PHP graduation project) obtain the campus network repair application management system based on PHP

浪涌冲击抗扰度实验(SURGE)-EMC系列 硬件设计笔记6

(PHP graduation project) based on PHP online travel website management system to obtain

Building neural network based on tensorflow

DSX2-8000如何校准?校准流程?

Common CTF encryption methods JS

1、 Speech synthesis and autoregressive model

Deep learning (I): enter the theoretical part of machine learning and deep learning
随机推荐
论福禄克DTX-1800如何测试CAT7网线?
初学者进行传感器选型
AEM online product promotion conference - Cable certification tester
51 single chip microcomputer independent key linkage nixie tube LED buzzer
GF(2^8)的加法与乘法计算
CString转char[]函数
For a large amount of data, matlab generates Excel files and typesetting processing source code
Paper reading notes of field low resolution face recognition based on selective knowledge extraction
Which enterprises are suitable for small program production and small program development?
PLC的选型
Neural network optimization
EIGamal cryptosystem description
The difference between router and switch
D2sc-gan: low resolution face recognition of classroom scenes based on dual depth and shallow channel generation confrontation network
arduino 读取模拟电压_MQ2气体/烟雾传感器如何工作及其与Arduino接口
8类网线测试仪AEM testpro CV100 和FLUKE DSX-8000哪些事?
AEM-TESTpro K50和南粤勘察结下的缘分
Tornado first met
Web滚动字幕(MARQUEE示例)
CVE_ 2017_ 11882 vulnerability recurrence (Metasploit opens NT remote desktop to add an account)