zoukankan      html  css  js  c++  java
  • 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;
                }
            }
        }

  • 相关阅读:
    Hadoop面试题总结(三)——MapReduce
    Hadoop面试题总结(二)——HDFS
    Hadoop面试题(一)
    html table有跨行跨列时,设置td宽度失效
    ubuntu14.04 安装五笔输入法(fcitx)
    NetCore生产环境禁用Swagger教程
    使用Jenkins与Docker持续集成与发布NetCore项目(实操篇)
    解决 Docker Unable to load the service index for source https://api.nuget.org/v3/index.json 问题
    .net webapi 接收图片保存到服务器,并居中剪裁压缩图片
    .net webapi 接收 xml 格式数据的三种情况
  • 原文地址:https://www.cnblogs.com/surfsky/p/1798908.html
Copyright © 2011-2022 走看看