[代码] DataGrid & GridView 使用区别

ASP.NET 2.0提供了功能强大的数据绑定控件GridView、在使用中,一些属性和方法经常会与ASP.NET 1.1中的DataGrid混淆(VS2005中依然可以使用DataGrid,手动添加到工具箱或HTML状态输入代码),下面我们分别用GridView和DataGrid实现其数据绑定、编辑、更新、删除等,从其代码中看两者的不同。
页面:

1 None.gif < asp:GridView  ID ="GridView1"  runat ="server"  AutoGenerateColumns ="False"  Width ="100%"  OnRowEditing ="GridView1_RowEditing"  OnRowCancelingEdit ="GridView1_RowCancelingEdit"  OnRowUpdating ="GridView1_RowUpdating"  DataKeyNames ="cat_id"  OnRowDeleting ="GridView1_RowDeleting" >
2 None.gif             < Columns >
3 None.gif                < asp:BoundField  DataField ="cat_tag"  HeaderText ="分类名称"   />                                         
4 None.gif                < asp:BoundField  DataField ="rec_dd"  HeaderText ="创建日期"   />          
5 None.gif                < asp:CommandField  ShowEditButton ="True"   />
6 None.gif                 < asp:CommandField  ShowDeleteButton ="True"   />
7 None.gif             Columns >
8 None.gif         asp:GridView >

1 None.gif   < asp:DataGrid  ID ="DataGrid1"  runat  ="server"  AutoGenerateColumns ="False"  Width ="100%"  OnCancelCommand ="DataGrid1_CancelCommand"  OnEditCommand ="DataGrid1_EditCommand"  OnUpdateCommand ="DataGrid1_UpdateCommand"  DataKeyField ="cat_id"  OnDeleteCommand ="DataGrid1_DeleteCommand"   >
2 None.gif         < Columns >
3 None.gif             < asp:BoundColumn  DataField ="cat_tag"  HeaderText ="分类名称" > asp:BoundColumn >
4 None.gif             < asp:BoundColumn  DataField ="rec_dd"  HeaderText ="创建日期"   > asp:BoundColumn >
5 None.gif             < asp:EditCommandColumn  CancelText ="取消"  EditText ="编辑"  UpdateText ="更新" > asp:EditCommandColumn >
6 None.gif             < asp:ButtonColumn  CommandName ="Delete"  Text ="删除" > asp:ButtonColumn >
7 None.gif         Columns >
8 None.gif     asp:DataGrid >
代码实现:
1 None.gif   //  数据绑定
2 None.gif      private   void  GridBind()
3 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
4InBlock.gif        dot.gifdot.gif.
5InBlock.gif        GridView1.DataSource = dt;
6InBlock.gif        GridView1.DataBind();
7InBlock.gif        DataGrid1.DataSource = dt;
8InBlock.gif        DataGrid1.DataBind();
9ExpandedBlockEnd.gif    }

1、GridView
 1 None.gif    //  编辑
 2 None.gif      protected   void  GridView1_RowEditing( object  sender, GridViewEditEventArgs e)
 3 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 4InBlock.gif        GridView1.EditIndex = e.NewEditIndex;
 5InBlock.gif        GridBind();
 6ExpandedBlockEnd.gif    }

 7 None.gif     // 取消
 8 None.gif      protected   void  GridView1_RowCancelingEdit( object  sender, GridViewCancelEditEventArgs e)
 9 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
10InBlock.gif        GridView1 .EditIndex  = -1;
11InBlock.gif        GridBind();
12ExpandedBlockEnd.gif    }

13 None.gif     // 更新
14 None.gif      protected   void  GridView1_RowUpdating( object  sender, GridViewUpdateEventArgs e)
15 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
16InBlock.gif        string id = GridView1.DataKeys[e.RowIndex][0].ToString();
17InBlock.gif        string newtxt = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
18InBlock.gif        string strSql = "UPDATE cat SET cat_tag='" + newtxt + "' WHERE cat_id=" + id;
19InBlock.gif        uc.ExecuteQuery(strSql);
20InBlock.gif        GridView1.EditIndex = -1;
21InBlock.gif        GridBind();
22ExpandedBlockEnd.gif    }

23 None.gif     // 删除
24 None.gif      protected   void  GridView1_RowDeleting( object  sender, GridViewDeleteEventArgs e)
25 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
26InBlock.gif        string id = GridView1.DataKeys[e.RowIndex][0].ToString();
27InBlock.gif        string strSql = "DELETE FROM cat WHERE cat_id=" + id;
28InBlock.gif        uc.ExecuteQuery(strSql);
29InBlock.gif        GridView1.EditIndex = -1;
30InBlock.gif        GridBind();
31ExpandedBlockEnd.gif    }
2、DataGrid
 1 None.gif   //  编辑
 2 None.gif      protected   void  DataGrid1_EditCommand( object  source, DataGridCommandEventArgs e)
 3 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
 4InBlock.gif        DataGrid1.EditItemIndex = e.Item.ItemIndex;
 5InBlock.gif        GridBind();
 6ExpandedBlockEnd.gif    }

 7 None.gif  // 取消
 8 None.gif      protected   void  DataGrid1_CancelCommand( object  source, DataGridCommandEventArgs e)
 9 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
10InBlock.gif        DataGrid1.EditItemIndex = -1;
11InBlock.gif        GridBind();
12ExpandedBlockEnd.gif    }

13 None.gif  // 更新
14 None.gif      protected   void  DataGrid1_UpdateCommand( object  source, DataGridCommandEventArgs e)
15 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
16InBlock.gif        string id = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
17InBlock.gif        string newtxt = ((TextBox)e.Item.Cells[0].Controls[0]).Text;
18InBlock.gif        string strSql = "UPDATE cat SET cat_tag='" + newtxt + "' WHERE cat_id=" + id;
19InBlock.gif        uc.ExecuteQuery(strSql);
20InBlock.gif        DataGrid1.EditItemIndex = -1;
21InBlock.gif        GridBind();
22ExpandedBlockEnd.gif    }

23 None.gif // 删除
24 None.gif      protected   void  DataGrid1_DeleteCommand( object  source, DataGridCommandEventArgs e)
25 ExpandedBlockStart.gifContractedBlock.gif     dot.gif {
26InBlock.gif        string id = DataGrid1 .DataKeys[e.Item .ItemIndex].ToString();
27InBlock.gif        string strSql = "DELETE FROM cat WHERE cat_id=" + id;
28InBlock.gif        uc.ExecuteQuery(strSql);
29InBlock.gif        DataGrid1.EditItemIndex = -1;
30InBlock.gif        GridBind();
31ExpandedBlockEnd.gif    }

你可能感兴趣的:([代码] DataGrid & GridView 使用区别)