[代码] DataGrid & GridView 使用区别

本文对比了ASP.NET 2.0中的GridView与1.1版本中的DataGrid控件在数据绑定、编辑、更新及删除操作上的差异。通过具体的代码实例展示了两者之间的不同之处。

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

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

1None.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" >
2None.gif        <Columns>
3None.gif            <asp:BoundColumn DataField="cat_tag" HeaderText="分类名称"></asp:BoundColumn>
4None.gif            <asp:BoundColumn DataField="rec_dd" HeaderText="创建日期" ></asp:BoundColumn>
5None.gif            <asp:EditCommandColumn CancelText="取消" EditText="编辑" UpdateText="更新"></asp:EditCommandColumn>
6None.gif            <asp:ButtonColumn CommandName="Delete" Text="删除"></asp:ButtonColumn>
7None.gif        </Columns>
8None.gif    </asp:DataGrid>
代码实现:
1None.gif // 数据绑定
2None.gif    private void GridBind()
3ExpandedBlockStart.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
 1None.gif  // 编辑
 2None.gif    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
 3ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 4InBlock.gif        GridView1.EditIndex = e.NewEditIndex;
 5InBlock.gif        GridBind();
 6ExpandedBlockEnd.gif    }

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

13None.gif    //更新
14None.gif    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
15ExpandedBlockStart.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    }

23None.gif    //删除
24None.gif    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
25ExpandedBlockStart.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
 1None.gif // 编辑
 2None.gif    protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
 3ExpandedBlockStart.gifContractedBlock.gif    dot.gif{
 4InBlock.gif        DataGrid1.EditItemIndex = e.Item.ItemIndex;
 5InBlock.gif        GridBind();
 6ExpandedBlockEnd.gif    }

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

13None.gif //更新
14None.gif    protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
15ExpandedBlockStart.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    }

23None.gif//删除
24None.gif    protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
25ExpandedBlockStart.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    }

转载于:https://www.cnblogs.com/winner/archive/2007/01/31/635727.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值