DataGrid与GridView

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值