1、新建一个空项目,添加Web窗体FindUser,在前台设计中拖入GridView控件,在编辑列中设计BoundField,HeaderText属性设置表头文本,DataField属性设置为要显示的数据库字段。
如果要加入修改,删除按钮,就再一次编辑列添加ButtonField,属性HeaderText设置表头文本,属性Text为样式显示的文字,设置各自的CommandName(Update、Delete),ButtonType也可以自己设置,可以是Button,也可以是超链接。
编辑列操作效果:
将GridView控件中设置AutoGenerateColumns属性为false,添加事件OnRowDeleting、OnRowUpdating、OnRowCommand
第一步结束后的前台代码:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDeleting="GridView1_RowDeleting" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField HeaderText="序号" DataField="ID" ReadOnly="true"/>
<asp:BoundField HeaderText="名称" DataField="Name" ReadOnly="true"/>
<asp:BoundField HeaderText="作者" DataField="Author" ReadOnly="true"/>
<asp:BoundField HeaderText="类型" DataField="Type" ReadOnly="true"/>
<asp:ButtonField ButtonType="Button" HeaderText="删除功能" Text="删除" CommandName="Delete"/>
<asp:ButtonField ButtonType="Button" HeaderText="修改功能" Text="修改" CommandName="Update"/>