• 从GridView生成DataTable


    DataTable与GridView从数据结构上来看都是一个由row和column组成表的结构,我们大部分时间是将DataTable绑定到 GridView中,但web中当页面回传的时候,传递给GridView的数据源却再也找不到了,这是一件很郁闷的事情,下面我们根据两者的相似性,实现从GridView生成DataTable的方法,不管原来的GridView数据源是否是DataTable,都能使用该方法
    /**//*----------------------------------------------------------------
    // 文件名: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;
            }
            /**////
            /// 从GridView的数据生成DataTable
            ///
            /// GridView对象
            public static DataTable GridView2DataTable(GridView gv)
            {
                DataTable table = new DataTable();
                int rowIndex = 0;
                List cols = new List();
                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);
                    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);
                            if (!String.IsNullOrEmpty(text))
                            {
                                if (rowIndex == 0)
                                {
                                  string columnName = cols;
                                    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;
  • 相关阅读:
    如何在文本编辑器中实现搜索功能? 字符串比较算法 BF算法 RK算法
    怎么读源码 读源码的一些技巧
    系统性学习
    堆 二叉堆 找流的中位数
    apk系统签名小技巧
    常用adb命令总结
    Android6.0 源码修改之Setting列表配置项动态添加和静态添加
    AndroidStudio开发Java工程(解决java控制台中文打印乱码+导入jar包运行工程)
    加载loading对话框的功能(不退出沉浸式效果)
    Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar
  • 原文地址:https://www.cnblogs.com/myssh/p/1390931.html
Copyright © 2020-2023  润新知