惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
AI
AI
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Docker
宝玉的分享
宝玉的分享
Hugging Face - Blog
Hugging Face - Blog
云风的 BLOG
云风的 BLOG
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
J
Java Code Geeks
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Y
Y Combinator Blog
P
Palo Alto Networks Blog
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
B
Blog
大猫的无限游戏
大猫的无限游戏
L
LINUX DO - 热门话题
Cisco Talos Blog
Cisco Talos Blog
IT之家
IT之家
Engineering at Meta
Engineering at Meta
C
CERT Recently Published Vulnerability Notes
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
F
Fortinet All Blogs
Jina AI
Jina AI
W
WeLiveSecurity
Hacker News: Ask HN
Hacker News: Ask HN
GbyAI
GbyAI
L
LangChain Blog
P
Privacy International News Feed
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
Project Zero
Project Zero
TaoSecurity Blog
TaoSecurity Blog
A
About on SuperTechFans
N
News | PayPal Newsroom
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
I
InfoQ
K
Kaspersky official blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Troy Hunt's Blog
博客园_首页
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
I
Intezer
博客园 - 聂微东

博客园 - mapserver

MicroOrm.Net和现有ORM产品的对比 MicroOrm.Net(8) Table.Query() - Distinct、Skip & Take MicroOrm.Net(7) Table.Query() - Group By & Having MicroOrm.Net(6) Table.Query() - Join & Order By MicroOrm.Net(5) Table.Query() - Select MicroOrm.Net(4) Table.Query() - Where MicroOrm.Net(3) Database、Table、Column、Expression、Math&MathE MicroOrm.Net(2) 基础及动态特性 MicroOrm.Net(1) 总览 Asp.net服务器控件编程(6) ViewState(二)——ViewState用法和IStateManager Asp.net服务器控件编程(5) 复杂属性 Asp.net服务器控件编程(4) 呈现 Asp.net服务器控件编程(3) ViewState(一)——asp.net控件的精华之一 Asp.net服务器控件编程(2) 来做个热身运动吧 Asp.net服务器控件编程(1) 开篇、基础 Asp.net服务器控件编程 总览 TextDataSource(3) — 请把我的数据更新回去 TextDataSource(1) — DataSourceControl内幕 .NET组件编程(10) 补充 ISupportInitialize
TextDataSource(2) — 翠花,上“数据”
mapserver · 2006-04-20 · via 博客园 - mapserver

        本篇文章参考了msdn的DataSourceControl相关帮助。
        上一章我们一起讨论了,DataSourceControl、IDataSource的基础,那我们今天来看如何把一个文本文件里的数据显示出来,数据的Insert、Update、Delete我们将在下章中进行讲解。
        所有继承于DataBoundControl 类绑定到数据显示控件(如DataGrid)时,它会根据数据显示控件的DataSource和DataMember为我们生成一个数据的View(也就是去执行DataSourceControl.GetView()方法),生成这个View就是我们显示数据的核心,它有点类似于DataView,当一个DataTable绑定到DataGrid上的时候,.NET会自动为我们生成一个View,也就是DefaultView,把这个呈现到DataGrid上用于显示。
        既然知道View是我们显示数据的核心(同时Insert、Update、Delete也是在其View上完成的),那又如何在定义DataSourceCotrol的View呢?其实也很简单,只要我们为自己的DataSourceControl定义的View继承于DataSourceView即可,我们来看看DataSourceView的主要成员:
        CanDelete   DataSourceControl 对象关联的 DataSourceView 对象是否支持 ExecuteDelete 操作。 
        CanInsert  DataSourceControl 对象关联的 DataSourceView 对象是否支持 ExecuteInsert 操作。 
        CanPage  当前 DataSourceControl 对象相关联的 DataSourceView 对象是否支持对通过 ExecuteSelect 方法检索到的数据进行分页。 
        CanUpdate  DataSourceControl 对象关联的 DataSourceView 对象是否支持 ExecuteUpdate 操作。 
        ExecuteDelete()  对DataSourceView 对象所表示的数据列表执行删除操作。  
        ExecuteInsert ()  对 DataSourceView 对象所表示的数据列表执行插入操作。  
        ExecuteSelect ()  从基础数据存储获取数据列表。  
        ExecuteUpdate()  对 DataSourceView 对象所表示的数据列表执行更新操作。

        以上的成员我们将会讲到,其余的成员请大家自己去查询msdn

using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Drawing.Design;
using System.Windows.Forms;

namespace TextDataSourceLib
{
    [AspNetHostingPermission(SecurityAction.Demand, Level 
= AspNetHostingPermissionLevel.Minimal)]
    
public class TextDataSource : DataSourceControl
    
{
        
public TextDataSource() : base() { }

        
private string _fileName = string.Empty;
        [Editor(
typeof(FileNameEditor), typeof(UITypeEditor))]
        
public string FileName  // 文件名称。
        {
            
get return _fileName; }
            
set { _fileName = value; }
        }


        
private string _separator = string.Empty;
        
// 分隔字符串。
        public string Separator
        
{
            
get return _separator; }
            
set { _separator = value; }
        }


        
// 根据名称得到DataSourceView
        protected override DataSourceView GetView(string viewName)
        
{
            
// 返回一个TextDataSourceView的时候,在TextDataSourceView里会自动去执行ExecuteSelect()方法。
            return new TextDataSourceView(this, _fileName);  
        }


        
protected override ICollection GetViewNames()
        
return new string[] { _fileName }; }
    }


    
// UITypeEditor的用法请参考:http://mapserver.cnblogs.com/archive/2006/03/08/345244.html
    public class FileNameEditor : UITypeEditor
    
{
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand)]
        
public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        
return UITypeEditorEditStyle.Modal; }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand)]
        
public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        
{
            IWindowsFormsEditorService editorService 
= (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            OpenFileDialog dialog 
= new OpenFileDialog();
            dialog.Multiselect 
= false;
            dialog.Filter 
= "Text Files(*.txt)|*.txt|All Files(*.*)|*.*";
            dialog.ShowDialog();
            
return dialog.FileName;
        }

    }


    
// 只要继承与DataSourceView即可。
    public class TextDataSourceView : DataSourceView
    
{
        
private string _fileName;
        
private TextDataSource _owner;
        
private string _separator;

        
public TextDataSourceView(IDataSource owner, string fileName)
            : 
base(owner, fileName)
        
{
            _owner 
= (TextDataSource)owner;
            _fileName 
= fileName;
            _separator 
= (_owner.Separator == string.Empty ? "," : _owner.Separator);
        }


        
// 数据显示、Update之前和Update完成之后也会去自动执行此方法,同理Insert、Delete也一样。
        protected override IEnumerable ExecuteSelect(DataSourceSelectArguments selectArgs)
        
{
            IEnumerable dataList 
= null;
            
if (File.Exists(this._fileName))
            
{
                DataTable table 
= new DataTable();
                
using (StreamReader sr = File.OpenText(this._fileName))
                
{
                    
string s = ""
                    
string[] dataValues; 
                    DataColumn column;

                    dataValues 
= sr.ReadLine().Split(_separator.ToCharArray());
                    
foreach (string value in dataValues)
                    
{
                        column 
= new DataColumn(value, typeof(string));
                        table.Columns.Add(column);
                    }


                    
while ((s = sr.ReadLine()) != null)
                    
{
                        dataValues 
= s.Split(_separator.ToCharArray());
                        table.Rows.Add(CopyRowData(dataValues, table.NewRow()));
                    }

                }

                table.AcceptChanges();      
// 把table里的数据的状态置为默认。

                DataView dataView 
= new DataView(table);    // 创建table的DataView用于显示。
                dataList = dataView;
            }

            
return dataList;
        }


        
private DataRow CopyRowData(string[] source, DataRow target)
        
{
            
try
            
{
                
for (int i = 0; i < source.Length; i++)
                
{ target[i] = source[i]; }
            }

            
catch (System.IndexOutOfRangeException)
            
{
                
return target;
            }

            
return target;
        }

    }

}


显示效果如下: