gaoyanhui-999

统计

最新评论

DataGrid控件

我今天学了许多东西.对于一些高手来说,这是非常容易的,但对于初学者来说.就是很有必要认真的学习.

来吧!!  一起分享一下吧.!!*^_^*



 
 DataGrid控件

DataGrid控件可用于创建各种样式的表格。它还支持对项目的选择和操作。下面的几个例子使用了包含如下字段的一个表:

 

Title

书名

Title ID

编号

Author

作者

Price

价格

Publication date

发行日期

 

这个表不在数据库中,而是保存在一个名为titlesdb.xml的文件中。我们将逐步给出完整的代码。

 

首先我们来看看titlesdb.xml的格式:

 

<root>

<schema id="DocumentElement" targetNamespace=""

        xmlns=http://www.w3.org/1999/XMLSchema

        xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

    <element name="Title">

        <complexType content="elementOnly">

            <element name="title_id" type="string"></element>

            <element name="title" type="string"></element>

            <element name="au_name" type="string"></element>

            <element name="price" msdata:DataType="System.Currency"

                     minOccurs="0"

                     type="string"></element>

            <element name="pubdate" type="timeInstant"></element>

        </complexType>

        <unique name="TitleConstraint" msdata:PrimaryKey="True">

            <selector>.</selector>

            <field>title_id</field>

        </unique>

    </element>

</schema>

<DocumentElement>

    <Title>

        <title_id>BU1032</title_id>

        <title>The Busy Executive's Database Guide</title>

        <au_name>Marjorie Green</au_name>

        <price>19.99</price>

        <pubdate>1991-06-12T07:00:00</pubdate>

    </Title>

    ...

</DocumentElement>

</root>

 

在一个典型的web应用中,很可能你需要使用web service或者商业部件来保证最大可能的可扩展性和性能。下面的例子为了简化代码,我们在global.asax中,通过响应application_onstart事件,读取xml数据到一个DataSet,然后缓存这个DataSet到一个Application变量。

 

代码如下:(global.asax)

 

public void Application_OnStart() {

    FileStream fs = null;

    DataSet ds = null;

 

    try {

        fs = new FileStream(Server.MapPath("TitlesDB.xml"), FileMode.Open,

                            FileAccess.Read);

        ds = new DataSet();

 

        // load the data in the xml file into the DataSet

        ds.ReadXml(fs);

    } finally {

        if (fs != null) {

            fs.Close();

            fs = null;

        }

    }

 

    // cache the dataset into application state for use in individual pages

    Application["TitlesDataSet"] = ds;

}

 

下面的代码产生了一个简单的页面:

 

dg01.aspx

<%@ Page language="C#" src="DataGrid.cs" inherits="Samples.DataGridPage"%>

...

 

<asp:DataGrid runat=server id="titlesGrid">

</asp:DataGrid>

 

 

(DataGrid.cs)

 

namespace Samples {

    ...

 

    public class DataGridPage : Page {

        protected DataGrid titlesGrid;

 

        public ICollection GetTitlesList() {

            // Retrieve the list of titles from the DataSet cached in

            // the application state.

            DataSet titlesDataSet = (DataSet)Application["TitlesDataSet"];

 

            if (titlesDataSet != null) {

                return titlesDataSet.Tables["Title"].DefaultView;

            }

            else {

                return null;

            }

        }

 

        private void LoadTitlesGrid() {

            // retrieve the data from the database

            ICollection titlesList = GetTitlesList();

 

            // set the control's datasource

            titlesGrid.DataSource = titlesList;

 

            // and have it build its items using the datasource

            titlesGrid.DataBind();

        }

 

        protected override void OnLoad(EventArgs e) {

            base.OnLoad(e);

 

            if (!IsPostBack) {

                // first request for the page

                LoadTitlesGrid();

            }

        }

    }

}

 

 

这个.cs文件包含了页面的所有代码。在功能上,这些代码和上一节的例子非常相似。通过重载页面的OnLoad方法,获得数据并绑定到DataGrid控件,实现了数据的显示。DataBind方法被调用时,DataGrid控件会根据DataSetDataTable的每一行,创建表格的每一行。当用户提交表单时,数据绑定不再被调用,控件将根据其原来的状态重新绘制每一个项目。

 

DataGridAutoGenerateColumns属性缺省是True。当AutoGenerateColumnsTrue 时,DataGrid将检查其数据源和其对象映射,并为每一个共有属性或者字段创建一个列。本例中,DataGrid控件把DataSet中的每一个字段显示为一个列。DataGrid的这种功能使得程序员使用很少的代码就可以使用DataGrid控件。

 

每一个自动产生的列称为一个BoundColumn(绑定列)。绑定列根据其数据表对应列的数据类型,自动将其转化为一个字符串,显示在表格的一个单元中。

 

我们来看看改进后的代码:

(dg02.aspx)

 

<%@ Page language="C#" src="DataGrid.cs" inherits="Samples.DataGridPage"%>

...

 

<asp:DataGrid runat=server id="titlesGrid"

     AutoGenerateColumns="false">

 <property name="Columns">

    <asp:BoundColumn HeaderText="Title" DataField="title"/>

    <asp:BoundColumn HeaderText="Author" DataField="au_name"/>

    <asp:BoundColumn HeaderText="Date Published" DataField="pubdate"/>

    <asp:BoundColumn HeaderText="Price" DataField="price"/>

 </property>

</asp:DataGrid>

 

 

dg02.aspx展示了用户自定义列集合的应用。由于采用了code-behind技术,DataGrid.cs可以不加任何修改。

 

这里,DataGridAutoGenerateColumns设置为false,不允许控件自动创建列集合。这样,DataGrid将应用用户定义的列集合来表现DataSet到一个表格中。

 

这样做有什么好处呢?

l         你可以控制列的顺序。表格的列将按照你给定的顺序排列。而相反地,自动产生的列将按照数据被存取的次序来创建,由于数据被存取的次序是不可指定的,他可能有别于代码中指定的顺序或者数据库中的顺序。

l         每一列的标题都可以指定。这可以通过指定其HeaderText属性来实现。在dg01.aspx中,列的标题缺省为字段名。在很多情况下,这不是你想要的。当然,你还可以使用BoundColumn的其他属性。

l         自动产生的列总是BoundColumn类型。而指定列允许使用继承了BoundColumn的用户控件。

 

Dg03.aspx是进一步的改进版本,它显示了如何控制DataGrid的外观表现和各个项目的格式化控制。

(dg03.aspx)

<%@ Page language="C#" src="DataGrid.cs" inherits="Samples.DataGridPage"%>

...

 

<asp:DataGrid runat=server id="titlesGrid"

     AutoGenerateColumns="false"

     Width="80%"

     BackColor="White"

     BorderWidth="1px" BorderStyle="Solid" CellPadding="2" CellSpacing="0"

     BorderColor="Tan"

     Font-Name="Verdana" Font-Size="8pt">

 <property name="Columns">

    <asp:BoundColumn HeaderText="Title" DataField="title"/>

    <asp:BoundColumn HeaderText="Author" DataField="au_name"/>

    <asp:BoundColumn HeaderText="Date Published" DataField="pubdate"

         DataFormatString="{0:MMM yyyy}"/>

    <asp:BoundColumn HeaderText="Price" DataField="price"

      DataFormatString="{0:c}">

      <property name="ItemStyle">

        <asp:TableItemStyle HorizontalAlign="Right"/>

      </property>

    </asp:BoundColumn>

 </property>

 

 <property name="HeaderStyle">

    <asp:TableItemStyle BackColor="DarkRed" ForeColor="White"

      Font-Bold="true"/>

 </property>

 <property name="ItemStyle">

    <asp:TableItemStyle ForeColor="DarkSlateBlue"/>

 </property>

  <property name="AlternatingItemStyle">

    <asp:TableItemStyle BackColor="Beige"/>

 </property>

</asp:DataGrid>

 

和前面的例子一样。不同的是,这里我们对DataGrid的样式属性进行了控制,从而得到了更好的表格外观。和dg02.aspx一样,DataGrid.cs不许要作任何的改进。

 

由于DataGrid是从WebControl继承来的,所以它也具有WidthBackColorBorderStyleFont等样式属性。此外,DataGrid还具有CellPadding等和表格关联的特殊属性。这些属性使程序员可以完全控制DataGrid的样式和表现。

 

这里还使用了HeaderStyleAlternatingItemStyle,这是和DataGrid项目相关的样式属性。这些属性可以控制表格项目的样式。本例中,表格的偶数行和奇数行具有同样的前景色,但是,偶数行的背景色不同于奇数行。本例还控制了Price一列的样式,使文本靠右对齐。

 

DataGrid还支持对表格单元的格式化控制。这是通过设置BoundColumnDataFormatString属性来实现的。这样,表格单元的内容将被String.Format方法所格式化。如果你不指定DataFormatString属性,缺省的ToString方法被调用。

 

Dg04.aspx显示了如何选择表格的一行:

(dg04.aspx)

<%@ Page language="C#" src="DataGrid4.cs" inherits="Samples.DataGrid4Page"%>

...

 

<asp:DataGrid runat=server id="titlesGrid"

     AutoGenerateColumns="false"

     Width="80%"

     BackColor="White"

     BorderWidth="1px" BorderStyle="Solid" CellPadding="2" CellSpacing="0"

     BorderColor="Tan"

     Font-Name="Verdana" Font-Size="8pt"

     DataKeyField="title_id"

     OnSelectedIndexChanged="OnSelectedIndexChangedTitlesGrid">

 <property name="Columns">

    <asp:ButtonColumn Text="Select" Command="Select"/>

    <asp:BoundColumn HeaderText="Title" DataField="title"/>

    <asp:BoundColumn HeaderText="Author" DataField="au_name"/>

    <asp:BoundColumn HeaderText="Date Published" DataField="pubdate"

         DataFormatString="{0:MMM yyyy}"/>

    <asp:BoundColumn HeaderText="Price" DataField="price"

      DataFormatString="{0:c}">

      <property name="ItemStyle">

        <asp:TableItemStyle HorizontalAlign="Right"/>

      </property>

    </asp:BoundColumn>

 </property>

 

 <property name="HeaderStyle">

    <asp:TableItemStyle BackColor="DarkRed" ForeColor="White"

      Font-Bold="true"/>

 </property>

 <property name="ItemStyle">

    <asp:TableItemStyle ForeColor="DarkSlateBlue"/>

 </property>

 <property name="AlternatingItemStyle">

    <asp:TableItemStyle BackColor="Beige"/>

 </property>

 <property name="SelectedItemStyle">

    <asp:TableItemStyle BackColor="PaleGoldenRod" Font-Bold="true"/>

 </property> 

</asp:DataGrid>

...

<asp:Label runat=server id="selectionInfoLabel" Font-Name="Verdana" Font-Size="8pt"/>

 

本例中,DataGridSelectedIndexChanged事件被处理,代码封装在下面的.cs文件中。和前面的例子不同,我们增加了一个具有“select”命令的按钮列。这就让DataGrid可以为每一行产生一个选择按钮。同时,SelectedItemStyle属性也被设置,这样可以很清楚地标志当前的选择项目。最后,DataKeyField属性得到指定,这是为了code-behind代码可以使用DataKeys集合。

 

现在我们来看看code-behind代码:

(DataGrid4.cs)

namespace Samples {

    ...

 

    public class DataGrid4Page : Page {

        protected DataGrid titlesGrid;

        protected Label selectionInfoLabel;

 

        public ICollection GetTitlesList() {

            // Retrieve the list of titles from the DataSet cached in

            // the application state.

            DataSet titlesDataSet = (DataSet)Application["TitlesDataSet"];

 

            if (titlesDataSet != null) {

                return titlesDataSet.Tables["Title"].DefaultView;

            }

            else {

                return null;

            }

        }

 

        private void LoadTitlesGrid() {

            // retrieve the data from the database

            ICollection titlesList = GetTitlesList();

 

            // set the control's datasource and reset its selection

            titlesGrid.DataSource = titlesList;

            titlesGrid.SelectedIndex = -1;

 

            // and have it build its items using the datasource

            titlesGrid.DataBind();

 

            // update the selected title info

            UpdateSelectedTitleInfo();

        }

 

        protected override void OnLoad(EventArgs e) {

            base.OnLoad(e);

 

            if (!IsPostBack) {

                // first request for the page

                LoadTitlesGrid();

            }

        }

 

        // Handles the OnSelectedIndexChanged event of the DataGrid

        protected void OnSelectedIndexChangedTitlesGrid(object sender,

                                                        EventArgs e) {

            UpdateSelectedTitleInfo();

        }

 

        private void UpdateSelectedTitleInfo() {

            // get the selected index

            int selIndex = titlesGrid.SelectedIndex;

            string selTitleID = null;

            string selectionInfo;

 

            if (selIndex != -1) {

                // display the key field for the selected title

                selTitleID = (string)titlesGrid.DataKeys[selIndex];

                selectionInfo = "ID of selected title: " + selTitleID;

            }

            else {

                selectionInfo = "No title is currently selected.";

            }

 

            selectionInfoLabel.Text = selectionInfo;

        }

    }

}

 

.cs文件包含了SelectedIndexChanged事件的处理逻辑,和显示当前选择的ID所需要的代码。当用户点击选择按钮时,SelectedIndexChanged事件就被激发。这时,DataGrid的标准命令”Select”被识别,其SelectIndex属性相应改变,然后,OnSelectedIndexChangedTitlesGrid得到执行。

 

OnSelectedIndexChangedTitlesGrid函数中,我们调用了UpdateSelectedTitleInfo方法。此方法负责显示当前选择项目的信息,此处为简单地显示ID号。当然,你可以根据这个ID关联的行,从而显示更多的信息。

 

ID是通过存取DataKeys集合获得的。因为在ASPX中指定了DataKeyField属性,我们可以使用这个集合。典型地,这个字段就是表的主键或者能够唯一确定一行的某个字段。根据这个字段,就可以获得当前选择项目的更具体信息。

 

本例显示了如何在显示数据之外,实现对数据的选择操作。DataGrid还具有很多其他特性,比如排序、分页、编辑、列模板等等。这里就不详细展开了。


好了,我把今天的学习已经共享了.

posted on 2007-09-02 12:02 高彦辉 阅读(496) 评论(1)  编辑 收藏 引用

只有注册用户登录后才能发表评论。