GridView DataGrid

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

1 < 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              < Columns >
3                 < asp:BoundField  DataField ="cat_tag"  HeaderText ="分类名称"   />                                         
4                 < asp:BoundField  DataField ="rec_dd"  HeaderText ="创建日期"   />          
5                 < asp:CommandField  ShowEditButton ="True"   />
6                  < asp:CommandField  ShowDeleteButton ="True"   />
7              </ Columns >
8          </ asp:GridView >

1   < 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          < Columns >
3              < asp:BoundColumn  DataField ="cat_tag"  HeaderText ="分类名称" ></ asp:BoundColumn >
4              < asp:BoundColumn  DataField ="rec_dd"  HeaderText ="创建日期"   ></ asp:BoundColumn >
5              < asp:EditCommandColumn  CancelText ="取消"  EditText ="编辑"  UpdateText ="更新" ></ asp:EditCommandColumn >
6              < asp:ButtonColumn  CommandName ="Delete"  Text ="删除" ></ asp:ButtonColumn >
7          </ Columns >
8      </ asp:DataGrid >
代码实现:
1   //  数据绑定
2      private   void  GridBind()
3      {
4        .
5        GridView1.DataSource = dt;
6        GridView1.DataBind();
7        DataGrid1.DataSource = dt;
8        DataGrid1.DataBind();
9    }

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

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

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

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

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

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

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

你可能感兴趣的:(datagrid,String,object,asp.net,asp,textbox)