Asp.net:GridView中的编辑 删除

首先拖入一个gridview控件,添加列,绑定数据。

在编辑时不可更改的列ReadOnly设置为true

PageSettings为设置gridview的格式。

添加commandField 设置删除键和编辑键可见。

<asp:GridView ID="gvNoFeeStu" runat="server" AutoGenerateColumns="False" OnRowEditing="gvNoFeeStu_RowEditing" OnRowDeleting="gvNoFeeStu_RowDeleting" OnRowUpdating = "gvNoFeeStu_RowUpdating" onselectedindexchanged="gvNoFeeStu_SelectedIndexChanged" onrowupdated="gvNoFeeStu_RowUpdated"> <Columns> <asp:BoundField DataField="college" HeaderText="学院" /> <asp:BoundField DataField="specialty" HeaderText="专业" /> <asp:BoundField DataField="stuNo" HeaderText="学号" ReadOnly="true" /> <asp:BoundField DataField="IsFee" HeaderText="是否缴费" /> <asp:BoundField DataField="defaultFee" HeaderText="拖欠费用" /> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" /> </Columns> <PagerSettings FirstPageText="" LastPageText="" NextPageText="" PreviousPageText="" /> <RowStyle Height="20px" BackColor="#F7F6F3" ForeColor="#333333" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#999999" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> </asp:GridView>后台代码,首先分清楚后台gridview的几个事件的区别。其实主要就是RowEditing RowEdited,RowDeleting RowDeleted 类似的还有RowUpdating RowUpdated

很明显ing是进行时,ed是过去时,在后台写代码的时候一定要分清楚这两个的区别,下面只写更新的代码,删除类似。

//定义未缴费学生实体 noFeeStudent objNoFee = new noFeeStudent(); noFeeStuManager noFeeStu = new noFeeStuManager(); //更新的值 objNoFee.College = ((TextBox)gvNoFeeStu.Rows[e.RowIndex].Cells[0].Controls[0]).Text; objNoFee.Specialty = ((TextBox)gvNoFeeStu.Rows[e.RowIndex].Cells[1].Controls[0]).Text; objNoFee.StuNo = ((TextBox)gvNoFeeStu.Rows[e.RowIndex].Cells[2].Controls[0]).Text; objNoFee.IsFee = ((TextBox)gvNoFeeStu.Rows[e.RowIndex].Cells[3].Controls[0]).Text; objNoFee.DefaultFee = decimal.Parse(((TextBox)gvNoFeeStu.Rows[e.RowIndex].Cells[4].Controls[0]).Text); //执行更新操作 noFeeStu.noFeeStuUpdate(objNoFee); //恢复 gvNoFeeStu.EditIndex = -1; //重新绑定

GridViewBind();

因为这些代码是在Updating中编写的,所以e.RowIndex为当前行的索引,如果在updated中就不能这样写了,那时的e.Rowindex就不一定为当前行了。

之前走了很多弯路,原因就是没有搞清楚这些方法和参数的意义,不求甚解,往往事倍功半。谨记这些教训。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值