zoukankan      html  css  js  c++  java
  • 从GridView生成DataTable

    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中


    示例代码:
    /Files/jillzhang/gv2dtdemo.rar

    更新标识:
    1. 2007-10-12 日 更新,添加对IButtonControl列的支持,修正了有时出现索引超出的异常

  • 相关阅读:
    禁用aspx页面的客户端缓存
    水晶报表的自动换行(转)
    ORACLE锁的管理
    同时使用有线和无线
    Oracle系统表的查询
    Oracle中临时表的深入研究
    我的My Life Rate
    [学习笔记]c#Primer中文版命名空间
    出差兰州·火车上
    [学习笔记]c#Primer中文版类设计、static成员、const和readonly数据成员
  • 原文地址:https://www.cnblogs.com/jillzhang/p/921058.html
Copyright © 2011-2022 走看看