GridView学习记录(一)

今天学习了GridView中的一些功能,主要包括数据的绑定,删除,更新。使用GridView 中的 RowDeleting, RowEditing,RowCancelingEditing,RowUpdating.

后台代码(以下代码并非完成代码,只是介绍每个事件,属性,方法应该如何使用)

1. GridView 的数据绑定涉及到的属性包括: DataSource, DataKeyNames(使用GridView中的事件 必须要设定这个数值) 。以及事件 DataBind()

代码如下:

1 private void GvBind()
2 {
3   DataTable dtCustome = testOperate.SearchReturnTable();//查询数据集,返回DataTable。
4   GridView.DataSource = dtCustome;
5   GridView.DataKeyNames = new string[] {"id"};//设置GridView中数据的主键字段。
6   GridView.DataBind();
7 }
GridView绑定数据

注意:如果使用GridView中的更新,删除必须设置DataKeyNames属性。

2. 删除数据,GridView删除数据需要用到RowDeleting事件。假设GridView控件名称:GridTest

代码如下:

1 protected void GridTest_RowDeleting(object sender, GridViewDeleteEvent e)
2 {
3      int id = (int) GridTest.DataKeys[e.RowIndex].Value;//如果绑定数据中没有设置DataKeyNames,此处系统会报错。
4     testOperate.DeleteInfo(id); //所写的删除信息方法。
5     GvBind();
6 }
GridView中RowDeleting事件

3.更新数据,GridView更新数据需要用到RowEditing, RowCancelingEditing与RowUpdating三个事件。

代码如下:

 1       protected void GridTest_RowEditing(object sender, GridViewEditEventArgs e)
 2 {
 3     GridTest.EditIndex = e.NewEditIndex;
 4     GvBind();
 5 }
 6 
 7       protected void GridTest_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
 8         {
 9             GridTest.EditIndex = -1;
10             GvBind();
11         }
12 
13         protected void GridTest_RowUpdating(object sender, GridViewUpdateEventArgs e)
14         {
15             
16             GridViewRow row = GridTest.Rows[e.RowIndex];
17             
18             //获得所在Row的主键值
19             DataKey key = GridTest.DataKeys[e.RowIndex];
20             int id = Convert.ToInt32(key[0].ToString());
21             
22              //需要更新的信息
23             string content = ((TextBox)(row.Cells[1].Controls[0])).Text;
24             string state =((TextBox)(row.Cells[3].Controls[0])).Text;            
25 
26              //编写的更新数据信息方法;
27              testOperate.UpdateInfo(content, Convert.ToInt32(state), id);
28              GVnotices.EditIndex = -1;
29              GvBind();
30             }
31         }
GridView更新数据

 

前台代码:

<asp:GridView ID="GVnotices" runat="server" AutoGenerateColumns="false" OnRowDeleting="GridTest_RowDeleting" OnRowEditing="GridTest_RowEditing" OnRowCancelingEdit="GridTest_RowCancelingEdit" OnRowUpdating="GridTest_RowUpdating">
                   <Columns>
                       <asp:BoundField DataField="id" HeaderText="编号" ReadOnly="true"/>
                       <asp:BoundField DataField="content" HeaderText="内容" ReadOnly="false"/>
                       <asp:BoundField DataField="creatNo" HeaderText="录入时间" ReadOnly="true" />
                       <asp:BoundField DataField="state" HeaderText="是否显示" ReadOnly="false"/> 
                       <asp:CommandField ShowCancelButton="true" ShowEditButton="true" ShowDeleteButton="true" EditText="编辑" UpdateText="更新" DeleteText="删除" CancelText="取消" HeaderText="操作" />                    
                   </Columns>
               </asp:GridView>

在前台代码中需要注意的几个问题:

1.如果希望自定义显示数据的顺序在GridView中 需要将 AutoGenerateColumns设置为False。否则绑定数据将按照传入DataTable中的顺序显示。

2.如果在更新中有不需要更新的列可将 <asp: BoundField>中的 ReadOnly设置为True, 默认情况为False;

3.如果希望见”编辑“,”删除“操作放在同一列下,只需添加一个<asp:CommandField>,并且设置 属性 ShowCancelButton, ShowEditButton, ShowDeleteButton为True。

4.更改显示Button 的内容可以通过EditText, UpdateText, DeleteText与CancelText来设置,设置列名可以通过HeaderText属性来设置。

 

以上内容为本人自己学习总结,或许有不足,不当之处各位指出。

 

posted @ 2017-12-20 12:02  僧正  阅读(222)  评论(0编辑  收藏  举报