在.net1.1里学习了datagrid,在.net2.0里又出现了gridview,今天初次学习了GRIDVIEW的基本操作,发现它比DATAGRID使用起来更加方便,功能更好。有了GRIDVIEW,连接数据库并在页面上操作数据显得非常简单,效率更高,一整套的数据显示、删除、更新等操作在短短的代码就可以完成。下面是我学习GRIDVIEW控件时的源代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview.aspx.cs" Inherits="gridview" %>
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="AccessDataSource1" DataKeyNames="stu_xh">
<Columns>
<asp:BoundField DataField="stu_xh" HeaderText="stu_xh" SortExpression="stu_xh" />
<asp:BoundField DataField="stu_pwd" HeaderText="stu_pwd" SortExpression="stu_pwd" />
<asp:BoundField DataField="stu_clsBDM" HeaderText="stu_clsBDM" SortExpression="stu_clsBDM" />
<asp:BoundField DataField="stu_sfzh" HeaderText="stu_sfzh" SortExpression="stu_sfzh" />
<asp:CommandField ShowDeleteButton="True" />
<asp:BoundField DataField="stu_name" HeaderText="stu_name" SortExpression="stu_name" />
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/ratvu_pj.mdb"
SelectCommand="SELECT [stu_xh], [stu_pwd], [stu_clsBDM], [stu_sfzh], [stu_name] FROM [student]" OnSelecting="AccessDataSource1_Selecting" DeleteCommand="delete from student where stu_xh=@stu_xh" UpdateCommand="update student set stu_name=@stu_name,stu_pwd=@stu_pwd where stu_xh=@stu_xh"
OldValuesParameterFormatString="a">
<UpdateParameters>
<asp:Parameter Name="stu_name" Type="String" />
<asp:Parameter Name="stu_pwd" Type="String" />
</UpdateParameters>
</asp:AccessDataSource>
</div>
</form>
</body>
</html>
红色部分是我以后要注意的,因为没有DataKeyNames我不能实现所有数据的操作,没有设置<pdateParameters>我就不能实现数据的正常更新,当时没注意这两点让我费了一些时间下次要注意了