Kevin Cheng's Yard
电脑是我的老婆,编程是我的灵魂,代码是我的语言,按键是我在歌唱。
随笔- 54  文章- 0  评论- 249 
博客园  首页  新随笔  联系  管理  订阅 订阅

DevExpress ASPxGridView 使用文档四:数据源

转载请注明出处:http://surfsky.cnblogs.com/

---------------------------------------------------------
-- DataSource 支持的数据源
--     DataTable
--     IList
--     BindingList
--     XXXDataSource
---------------------------------------------------------
DataTable
    grid.DataSource = dt;
    grid.DataBind();
   
IList
    int articleId = Convert.ToInt32(Request.QueryString["articleId"]);
    IList<BlogArticleImage> images = BlogArticleImage.ListArticleImages(articleId);
    this.gvImages.KeyFieldName = "ImageId";
    this.gvImages.DataSource = images;
    this.gvImages.DataBind();

BindingList
    private void CreateQuotes()
    {
        BindingList<Quote> res = new BindingList<Quote>();
        foreach(string name in names)
        {
            Quote q = new Quote(name);
            q.Value = (decimal)GetRandom().Next(800, 2000) / (decimal)10;
            res.Add(q);
        }
        Session["Quotes"] = res;
    }


AccessDataSource
    <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
        SelectCommand="SELECT * FROM [Customers]"
        DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = ?"
        InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        UpdateCommand="UPDATE [Customers] SET [CompanyName] = ?, [ContactName] = ?, [ContactTitle] = ?, [Address] = ?, [City] = ?, [Region] = ?, [PostalCode] = ?, [Country] = ?, [Phone] = ?, [Fax] = ? WHERE [CustomerID] = ?"
        />
    <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/nwind.mdb"
        OnDeleting="AccessDataSource1_Modifying" OnInserting="AccessDataSource1_Modifying" OnUpdating="AccessDataSource1_Modifying"
        SelectCommand="SELECT * FROM [Customers]" DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = ?"
        InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
        UpdateCommand="UPDATE [Customers] SET [CompanyName] = ?, [ContactName] = ?, [City] = ?, [Region] = ?, [Country] = ? WHERE [CustomerID] = ?">
        <DeleteParameters>
            <asp:Parameter Name="CustomerID" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="CompanyName" Type="String" />
            <asp:Parameter Name="ContactName" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="Region" Type="String" />
            <asp:Parameter Name="Country" Type="String" />
        </UpdateParameters>
    </asp:AccessDataSource>
    protected void AccessDataSource1_Modifying(object sender, SqlDataSourceCommandEventArgs e) {
       DemoSettings.AssertNotReadOnly();
    }
    用代码实现
        AccessDataSource ds = new AccessDataSource();
        ds.DataFile = AccessDataSource1.DataFile;
        ds.SelectCommand = "select Photo from [Employees] where employeeid=" + id;
        DataView view = (DataView)ds.Select(DataSourceSelectArguments.Empty);
        if(view.Count > 0) return view[0][0] as byte[];
        return null;

ObjectDataSource
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        TypeName="Quotes"
        SelectMethod="LoadQuotes"
        />
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        DataObjectTypeName="PersonRegistration"
        TypeName="MyPersonProvider"
        SelectMethod="GetList" UpdateMethod="Update" InsertMethod="Insert"
        />
    <asp:objectDataSource id="ObjectDataSource1" runat="server"
        typename="PersonManager"
        selectMethod="SelectPersons"
        deleteMethod="DeletePerson"
        updateMethod="UpdatePerson"
        insertMethod="InsertPerson" >
        <insertParameters>
            <asp:parameter name="Id" type="Int32" />
        </insertParameters>
    </asp:objectDataSource>
   
ObjectDataSource.Parameters
    <SelectParameters>
        <asp:SessionParameter Name="IGYSID" SessionField="ID" Type="Int32" />
        <asp:SessionParameter DefaultValue="0" Name="ICGFS" SessionField="ICGFS" Type="Int32" />
    </SelectParameters>
    <UpdateParameters>
        <asp:Parameter Name="IWZID" />
        <asp:Parameter Name="IGYSID" />
        <asp:Parameter Name="ICGMXID" />
        <asp:Parameter Name="IGYSBJID" />
        <asp:Parameter Name="NBJ" />
        <asp:Parameter Name="CBZ" />
    </UpdateParameters>
    <UpdateParameters>
        <asp:FormParameter FormField="makeid" Name="MakeID" Type="String" />
        <asp:FormParameter FormField="name" Name="Name" Type="String" />
        <asp:FormParameter FormField="id" Name="ID" Type="String" ConvertEmptyStringToNull="False" />
    </UpdateParameters>


ObjectDataSource 使用的类
    (以下代码整理并修改至《ASP.NET 2.0 Revealed》P71-P78)
    (另外一个购物篮的例子,参考P138)
    public class Person
    {
        private int id;
        private string firstName;
        private string lastName;

        public int Id {...}
        public string FirstName {...}
        public string LastName {...}
       
        public Person(int id, string firstName, string lastName)
        {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
    public class PersonCollection : List<Person> {}
    public class PersonManager
    {
        private const string personsKey = "persons";
       
        public PersonCollection SelectPersons()
        {
            HttpContext context = HttpContext.Current;
            if (context.Application[personKey] == null)
            {
                PersonCollection persons = new PersonCollection();
                persons.Add(new Person(0, "Patrick", "Lorenz"));
                persons.Add(new Person(0, "Patrick", "Lorenz"));
                persons.Add(new Person(0, "Patrick", "Lorenz"));
                persons.Add(new Person(0, "Patrick", "Lorenz"));
                context.Application[personKey] = persons;
            }
            return context.Application[personKey] as PersonCollection;
        }
       
        public Person SelectPerson(int id)
        {
            foreach (Person p in SelectPersons())
                if (p.Id == id)
                    return p;  
            return null;
        }
       
        public void DeletePerson(int id)
        {
            PersonCollection persons = (Application[personKey] as PersonCollections);
            Person person = SelectPerson(id);
            if (person != null)
                persons.Remove(person);
        }
       
        public void InsertPerson(int id, string firstName, string lastName)
        {
            PersonCollection persons = (Application[personKey] as PersonCollections);
            persons.Add(new Person(id, firstName, lastName));
        }
       
        public void UpdatePerson(int id, string firstName, string lastName)
        {
            Person person = SelectPerson(id);
            if (person != null)
            {
                person.FirstName = firstName;
                person.LastName = lastName;
            }
        }
    }

 

转载请注明出处:http://surfsky.cnblogs.com 

绿色通道:好文要顶关注我收藏该文与我联系
posted @ 2010-08-13 13:08 Kevin Cheng 阅读(624) 评论(0) 编辑 收藏
注册用户登录后才能发表评论,请 登录 或 注册,返回博客园首页。
首页博问闪存新闻园子招聘知识库
最新IT新闻:
· 谷歌将出售Clearwire股份 不到收购价1/10
· 为什么Google比苹果更令微软恐惧?
· 思科拟2.71亿美元收购私有公司Lightwire
· 社交商务公司Bazaarvoice将于2月24日进行IPO
· 戴尔收购备份软件公司AppAssure
» 更多新闻...
最新知识库文章:
· 领域模型管理与AOP
· 编程的艺术:漂亮的代码和漂亮的软件
· GIT分支管理是一门艺术
· 编程:是一门艺术
· 编程是一门艺术吗?
» 更多知识库文章...

China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
Copyright ©2012 Kevin Cheng
精于斯,乐于斯。
昵称:Kevin Cheng
园龄:6年3个月
粉丝:20
关注:1
<2010年8月>
日一二三四五六
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

搜索

 
 

常用链接

  • 我的随笔
  • 我的评论
  • 我的参与
  • 最新评论
  • 我的标签
  • 更多链接

我的标签

  • Rss RssItem RssChannel XmlSerializer(1)
  • MVC ASP.NET Razor(1)
  • MVC Razor ASP.NET(1)
  • MVC ASP.NET(1)
  • javascript oo 类 对象 命名空间(1)
  • ASP.NET MVC SKIN 换肤(1)
  • Silverlight WPF(1)

随笔分类

  • 日子(3)
  • .NET(14)
  • .NET组件控件(15)
  • IT新闻(1)
  • 报表开发(1)
  • 代码生成器(1)
  • 工作流引擎
  • 建模与快速开发 (5)
  • 数据库 (4)
  • 杂项(7)

随笔档案

  • 2010年12月 (1)
  • 2010年11月 (1)
  • 2010年10月 (6)
  • 2010年8月 (7)
  • 2010年6月 (3)
  • 2009年12月 (1)
  • 2009年11月 (2)
  • 2009年5月 (2)
  • 2008年12月 (1)
  • 2008年8月 (1)
  • 2008年7月 (1)
  • 2007年12月 (1)
  • 2007年6月 (1)
  • 2007年5月 (1)
  • 2007年3月 (2)
  • 2007年1月 (1)
  • 2006年12月 (1)
  • 2006年11月 (1)
  • 2006年10月 (1)
  • 2006年9月 (1)
  • 2006年8月 (3)
  • 2006年6月 (5)
  • 2006年3月 (2)
  • 2006年2月 (1)
  • 2005年12月 (7)

文章分类

  • .NET(1)

相册

  • 回忆

Blogs

  • DbToCode
  • RapidTier
  • SmartPersistenceLayer
  • 灵感之源

NET WebSite

  • ASP.NET
  • CodeProject
  • CSDN
  • GoDotNet
  • MSDN
  • SourceForge

Special

  • icsharpcode.com
  • Open License
  • Python

最新评论

阅读排行榜

评论排行榜

推荐排行榜