GridView控件显示数据综合实例

来源:互联网 发布:大金融数据行业应用 编辑:程序博客网 时间:2024/06/02 20:47

首先先在一个数据库中创建两个表,分别是grade和student。

grade数据表 Id char 10 主键 Name vchar 50   Chinese real 4   Mathematics real 4   English real 4   Political real 4   Average real 4  

student数据表 学号 char 10 主键 通讯地址 text 16   邮政编码 char 10   电话 char 18   Email nchar 20   备注 ntext 16  

接着是两个Web窗体,分别是GridView01.aspx和GridView02.aspx。以下为各部分代码:

GridView01.aspx:

<head runat="server">
    <title>GridView控件显示数据的综合实例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align=center>
        <asp:Label ID="Label1" runat="server" Text="GridView显示数据综合实例" Height="24px" Width="316px"></asp:Label>
        <p></p>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="206px" Width="331px" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5">
        <Columns>
        <asp:HyperLinkField DataNavigateUrlFields="Id" DataNavigateUrlFormatString="GridView02.aspx?sID={0}" DataTextField="Name" HeaderText="姓名" />
        <asp:BoundField DataField="Average" HeaderText="平均分" />
        </Columns>
            <PagerSettings PageButtonCount="3" />
        </asp:GridView>
   
    </div>
    </form>
</body>

GridView01.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strConn = "server=localhost;uid=sa;pwd=860712;database=MMS";
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = strConn;
        string strCommand = "select * from grade";
        SqlDataAdapter da = new SqlDataAdapter(strCommand, myConnection);
        DataSet ds = new DataSet();
        da.Fill(ds, "scores");
        GridView1.DataSource = ds.Tables["scores"].DefaultView;
        GridView1.DataBind();
    }  
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
}

GridView02.aspx:

<head runat="server">
    <title>学生档案</title>
</head>
<body text="#0000">
    <form id="form1" runat="server">
    <div align=center>
        <asp:Label ID="Label1" runat="server" Text="学生档案" Height="28px" Width="104px"></asp:Label>
        <p></p>
        <asp:GridView ID="GridView1" runat="server" Height="163px" Width="497px" CellPadding="4" ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
   
    </div>
        <table align=center style="width: 500px; height: 240px">
            <tr>
                <td style="width: 100px">
                </td>
                <td style="width: 400px;">
                </td>
            </tr>
            <tr>
                <td style="width: 100px" align="left">
                    通讯地址:</td>
                <td style="width: 400px">
                    <asp:Label ID="Label2" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 100px" align="left">
                    邮政编码:</td>
                <td style="width: 400px">
                    <asp:Label ID="Label3" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 100px" align="left">
                    电话:</td>
                <td style="width: 400px">
                    <asp:Label ID="Label4" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 100px;">
                    Email:</td>
                <td style="width: 400px;">
                    <asp:Label ID="Label5" runat="server"></asp:Label></td>
            </tr>
            <tr>
                <td style="width: 100px;" align="left">
                    备注:</td>
                <td style="width: 400px;">
                    <asp:Label ID="Label6" runat="server"></asp:Label></td>
            </tr>
        </table>
    </form>
</body>

GridView02.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GridView02 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strConn = "server=localhost;uid=sa;pwd=860712;database=MMS";
        SqlConnection myConnection = new SqlConnection();
        myConnection.ConnectionString = strConn;
        string strCommand1 = "select Id '学号',Name '姓名',Chinese '语文',Mathematics '数学',English '英语',Political '政治',Average '平均分' from grade where Id like '"+Request["sID"]+"'";
        SqlDataAdapter da1 = new SqlDataAdapter(strCommand1, myConnection);
        DataSet ds = new DataSet();
        da1.Fill(ds, "scores");
        GridView1.DataSource = ds.Tables["scores"].DefaultView;
        GridView1.DataBind();

        string strCommand2 = "select 通讯地址,邮政编码,电话,Email,备注 from student where 学号 like '" + Request["sID"] + "'";
        SqlDataAdapter da2 = new SqlDataAdapter(strCommand2, myConnection);
        da2.Fill(ds, "adress");

        Label2.Text = ds.Tables["adress"].Rows[0][0].ToString();
        Label3.Text = ds.Tables["adress"].Rows[0][1].ToString();
        Label4.Text = ds.Tables["adress"].Rows[0][2].ToString();
        Label5.Text = ds.Tables["adress"].Rows[0][3].ToString();
        Label6.Text = ds.Tables["adress"].Rows[0][4].ToString();
    }
}

 

原创粉丝点击