datagridview控件用法,[代码] DataGrid GridView 使用区别

 2023-09-21 阅读 20 评论 0

摘要:ASP.NET 2.0提供了功能强大的数据绑定控件GridView、在使用中,一些属性和方法经常会与ASP.NET 1.1中的DataGrid混淆(VS2005中依然可以使用DataGrid,手动添加到工具箱或HTML状态输入代码),下面我们分别用GridView和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/chy710/archive/2007/01/29/633947.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/81251.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息