GridView控件显示数据综合实例
来源:互联网 发布:大金融数据行业应用 编辑:程序博客网 时间:2024/06/02 20:47
首先先在一个数据库中创建两个表,分别是grade和student。
接着是两个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();
}
}
- GridView控件显示数据综合实例
- 利用GridView控件显示数据
- GridView控件分页显示数据
- 如何实现GridView控件分页显示数据
- 使用GridView控件分页显示数据
- 使用服务器端数据验证控件的综合实例
- 使用Repeater控件进行数据访问的综合实例
- 使用DataList控件进行数据访问的综合实例
- Dev控件综合实例(后台数据操作,控件数据级联更新,前后台异步交互)
- Dev控件综合实例(后台数据操作,控件数据级联更新,前后台异步交互)
- 扩展 GridView 控件 - 无数据时显示标题列
- 数据绑定控件GridView
- 控件(七)——GridView控件以SqlDataSource控件为数据源显示数据
- 绑定GridView控件时,当数据集DataSet为空时也显示GridView的表头
- gridview动态显示数据
- GridView 数据显示格式
- GridView分页显示数据
- GridView显示数据
- 成功的人生应记住这10个故事
- Thunderbird 2.0.0.0配置手册
- 影响一生的忠告
- 三谈为什么说SAP套装解决方案更适合企业管理信息系统建设
- Groovy项目获大奖
- GridView控件显示数据综合实例
- 开博啦
- 真实的中国与日本、德国最可怕的差距(转载)
- 三种SQL分页法
- 保护好你的妻子
- 复制Windows用户配置文件
- 使用Zoundry Blog Writer写BLOG
- 设计模式学习笔记(一)——Singleton
- Repeater 第一条显示图片,其它条不显示