从GridView生成DataTable

来源: http://www.cnblogs.com/jillzhang/archive/2007/10/11/921058.html 作者:Robin's Space
DataTable与GridView从数据结构上来看都是一个由row和column组成表的结构,我们大部分时间是将DataTable绑定到GridView中,但web中当页面回传的时候,传递给GridView的数据源却再也找不到了,这是一件很郁闷的事情,下面我们根据两者的相似性,实现从GridView生成DataTable的方法,不管原来的GridView数据源是否是DataTable,都能使用该方法
/**//*----------------------------------------------------------------
// Copyright (C) 2007 jillzhang 版权所有。
// 
// 文件名:GridView.cs
// 文件功能描述:
//
// 创建标识:jillzhang
// 修改标识:
// 修改描述:
//
// 修改标识:
// 修改描述:
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI;

namespace jzlib.Common
{
    public class GridViewHelper
    {
        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 IButtonControl)
                {
                    IButtonControl btn = control as IButtonControl;
                    text = btn.Text.Replace("\r\n", "").Trim();
                    break;
                }
                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;
        }
        /**//// <summary>
        /// 从GridView的数据生成DataTable
        /// </summary>
        /// <param name="gv">GridView对象</param>
        public static DataTable GridView2DataTable(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)
                            {
                               string columnName = cols[i];
                                if (String.IsNullOrEmpty(columnName))
                                {       
                                        continue;
                                }
                                if (table.Columns.Contains(columnName))
                                {
                                    continue;
                                }
                                DataColumn dc = table.Columns.Add();
                                dc.ColumnName = columnName;
                                dc.DataType = typeof(string);
                            }
                            row[j] = text;
                            j++;
                        }                   
                    }
                    rowIndex++;
                    table.Rows.Add(row);
                }
            }
            return table;
        }
    }
}
使用这个函数,您可以得到GridView当前页面的数据,当然如果您在GridView中添加了复杂的控件,我们将略过这些内容,我们只从显示文本的列中导出数据到DataTable,如果您想导出GridView的全部数据,请在绑定前设置AllowPaging=false;
当然大家会问这样做有什么用呢?这个函数是我在开发将GridView导出Excel的时候想到的,类似于这种应用情形,我想还有很多。
下面是一个示例,通过从上面的GridView导出DataTable,然后再绑定到下面的GridView中
 

相关文档推荐

新零售行业Agent解决方案.PDF

1742957777  6.72MB 34页 积分5

B2B市场人DeepSeekAI提示词手册.PDF

1742949832  2.93MB 26页 积分6

AIGC如何助力工作和学习.PDF

1742949482 尹健 10.53MB 93页 积分8

DeepSeek政务应用场景与解决方案.PDF

1742949439  3.03MB 34页 积分6

2025年央国企信创数字化研究报告.PDF

1742809441  4.72MB 55页 积分5

2024年中国营销行业AI应用发展研究报告.PDF

1742803952  2.8MB 29页 积分4

AI落地应用最新工具集.PDF

1742450890  1.7MB 8页 积分4

DeepSeek完全实用手册.PDF

1742450791  3.62MB 114页 积分10

离散制造破局之道主数据管理平台重构.PDF

1742450737 詹慧超 4.6MB 37页 积分6

DeepSeek提示词设计、幻觉避免与应用.PDF

1742351308 程希冀 2.5MB 47页 积分6

相关文章推荐