用 GridView 内建的编辑功能更新数据库
2008-07-23 21:25
579 查看
(本帖在版工的旧 Blog 中,发表日期为 2007/09/04)
版工之前觉得 ASP.NET 2.0 GridView 内建的编辑功能 (新增、修改、删除),其界面很丑又不实用 (如下图 1),从 .NET 1.x (DataGrid) 后就没再用过了。但日前开发 ASP.NET 2.0 project,临时要用到这个功能,倒也费了版工一些时间摸索其用法;虽然很多书上都有介绍过该编辑功能了,版工仍将试成功的关键源代码贴出,以便日后参考。执行画面如下图 1, GridView 要透过 SqlDataSource 去 SQL Server 2005 做新增、修改、删除,新增动作的执行,是透过右上方的「新增出货明细」Button。
要透过 GridView 更新的数据表,其 Primary Key 为「PGID」、「sn」两个 column 所组成的「复合主键」,前者的类型为 nvarchar、后者的类型为 int;要开放给 user 更新的「净重(KG)」字段,其 column name 为「Weight」,其类型为 decimal。图中 GridView 里的「疋数」,是由一支 Stored Procedure 自动产生累加编号 (Identity),其值并不会存至数据库中。
<asp:GridView ID="GridView1" runat="server" AllowPaging="False" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PGID,sn" EmptyDataText="查无数据" DataSourceID="sdsGridView" CellPadding="3" ForeColor="#333333" GridLines="Vertical" width="310px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" UpdateText="存储" ItemStyle-HorizontalAlign="Center">
<ItemStyle Width="70px" />
</asp:CommandField>
<asp:BoundField DataField="存储过程产生的自动累加编号字段名称" HeaderText="疋數" SortExpression="存储过程产生的自动累加编号字段名称" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Right" ReadOnly="true" />
<asp:BoundField DataField="Weight" HeaderText="净重(KG)" SortExpression="Weight" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Right" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<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="#333333" />
</asp:GridView>
<asp:SqlDataSource ID="sdsGridView" runat="server" ConflictDetection="CompareAllValues"
InsertCommand="INSERT INTO 数据表 (PGID,sn,Weight,insertdate,updatetime) VALUES(@PGID,@sn,@Weight,getdate(),getdate())"
OldValuesParameterFormatString="original_{0}"
ConnectionString="<%$ ConnectionStrings:数据表连结字符串 %>"
SelectCommand="存储过程名称" SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE [数据表] SET Weight=@Weight WHERE [PGID]=@original_PGID AND [sn]=@original_sn"
DeleteCommand="DELETE FROM [数据表] WHERE [PGID]=@original_PGID AND [sn]=@original_sn"
>
<SelectParameters>
<asp:QueryStringParameter Name="PGID" QueryStringField="PGID" Type="String" />
<asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="PGID" Type="String" />
<asp:Parameter Name="sn" Type="Int32" />
<asp:Parameter Name="Weight" Type="Decimal" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Weight" Type="Decimal" />
<asp:Parameter Name="original_PGID" Type="String" />
<asp:Parameter Name="original_sn" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="PGID" Type="String" />
<asp:Parameter Name="sn" Type="Int32" />
<asp:Parameter Name="original_PGID" Type="String" />
<asp:Parameter Name="original_sn" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
// GridView上方的「新增出货明细」
protected void btnNew_Click(object sender, EventArgs e)
{
string strGetMaxSnOfThisPGID = "SELECT isnull(MAX(sn),0)+1 FROM [数据表] WHERE PGID='" + Request.QueryString["PGID"] +"'";
int intMaxSnPlusOne = Access_Db.selectOnlyOneInt(strGetMaxSnOfThisPGID);
((SqlDataSource)FormViewMaster.FindControl("sdsGridView")).InsertParameters["PGID"].DefaultValue = Request.QueryString["PGID"];
((SqlDataSource)FormViewMaster.FindControl("sdsGridView")).InsertParameters["sn"].DefaultValue = intMaxSnPlusOne.ToString();
((SqlDataSource)FormViewMaster.FindControl("sdsGridView")).InsertParameters["Weight"].DefaultValue = "";
((SqlDataSource)FormViewMaster.FindControl("sdsGridView")).Insert();
((GridView)FormViewMaster.FindControl("GridView1")).EditIndex = ((GridView)FormViewMaster.FindControl("GridView1")).Rows.Count;
}
版工之前觉得 ASP.NET 2.0 GridView 内建的编辑功能 (新增、修改、删除),其界面很丑又不实用 (如下图 1),从 .NET 1.x (DataGrid) 后就没再用过了。但日前开发 ASP.NET 2.0 project,临时要用到这个功能,倒也费了版工一些时间摸索其用法;虽然很多书上都有介绍过该编辑功能了,版工仍将试成功的关键源代码贴出,以便日后参考。执行画面如下图 1, GridView 要透过 SqlDataSource 去 SQL Server 2005 做新增、修改、删除,新增动作的执行,是透过右上方的「新增出货明细」Button。
要透过 GridView 更新的数据表,其 Primary Key 为「PGID」、「sn」两个 column 所组成的「复合主键」,前者的类型为 nvarchar、后者的类型为 int;要开放给 user 更新的「净重(KG)」字段,其 column name 为「Weight」,其类型为 decimal。图中 GridView 里的「疋数」,是由一支 Stored Procedure 自动产生累加编号 (Identity),其值并不会存至数据库中。
<asp:GridView ID="GridView1" runat="server" AllowPaging="False" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="PGID,sn" EmptyDataText="查无数据" DataSourceID="sdsGridView" CellPadding="3" ForeColor="#333333" GridLines="Vertical" width="310px">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" UpdateText="存储" ItemStyle-HorizontalAlign="Center">
<ItemStyle Width="70px" />
</asp:CommandField>
<asp:BoundField DataField="存储过程产生的自动累加编号字段名称" HeaderText="疋數" SortExpression="存储过程产生的自动累加编号字段名称" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Right" ReadOnly="true" />
<asp:BoundField DataField="Weight" HeaderText="净重(KG)" SortExpression="Weight" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Right" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<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="#333333" />
</asp:GridView>
<asp:SqlDataSource ID="sdsGridView" runat="server" ConflictDetection="CompareAllValues"
InsertCommand="INSERT INTO 数据表 (PGID,sn,Weight,insertdate,updatetime) VALUES(@PGID,@sn,@Weight,getdate(),getdate())"
OldValuesParameterFormatString="original_{0}"
ConnectionString="<%$ ConnectionStrings:数据表连结字符串 %>"
SelectCommand="存储过程名称" SelectCommandType="StoredProcedure"
UpdateCommand="UPDATE [数据表] SET Weight=@Weight WHERE [PGID]=@original_PGID AND [sn]=@original_sn"
DeleteCommand="DELETE FROM [数据表] WHERE [PGID]=@original_PGID AND [sn]=@original_sn"
>
<SelectParameters>
<asp:QueryStringParameter Name="PGID" QueryStringField="PGID" Type="String" />
<asp:Parameter Direction="ReturnValue" Name="RETURN_VALUE" Type="Int32" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="PGID" Type="String" />
<asp:Parameter Name="sn" Type="Int32" />
<asp:Parameter Name="Weight" Type="Decimal" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="Weight" Type="Decimal" />
<asp:Parameter Name="original_PGID" Type="String" />
<asp:Parameter Name="original_sn" Type="Int32" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="PGID" Type="String" />
<asp:Parameter Name="sn" Type="Int32" />
<asp:Parameter Name="original_PGID" Type="String" />
<asp:Parameter Name="original_sn" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
// GridView上方的「新增出货明细」
protected void btnNew_Click(object sender, EventArgs e)
{
string strGetMaxSnOfThisPGID = "SELECT isnull(MAX(sn),0)+1 FROM [数据表] WHERE PGID='" + Request.QueryString["PGID"] +"'";
int intMaxSnPlusOne = Access_Db.selectOnlyOneInt(strGetMaxSnOfThisPGID);
((SqlDataSource)FormViewMaster.FindControl("sdsGridView")).InsertParameters["PGID"].DefaultValue = Request.QueryString["PGID"];
((SqlDataSource)FormViewMaster.FindControl("sdsGridView")).InsertParameters["sn"].DefaultValue = intMaxSnPlusOne.ToString();
((SqlDataSource)FormViewMaster.FindControl("sdsGridView")).InsertParameters["Weight"].DefaultValue = "";
((SqlDataSource)FormViewMaster.FindControl("sdsGridView")).Insert();
((GridView)FormViewMaster.FindControl("GridView1")).EditIndex = ((GridView)FormViewMaster.FindControl("GridView1")).Rows.Count;
}
相关文章推荐
- EnableViewState问题,在用gridview的编辑功能更新数据时候出错
- 详解GridView自带的编辑删除更新功能
- 使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
- 手工实现GridView排序、删除、编辑、新增数据功能
- 新增格式化单元格数据功能-dotNetFlexGrid更新1.24版本,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。
- GridView的编辑,更新,取消,删除等功能演示
- 新增格式化单元格数据功能-dotNetFlexGrid1.24,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。(更新FieldFormatorHandle参数)
- 新增格式化单元格数据功能-dotNetFlexGrid1.24,快速的设置您的表格数据的不同样式;现在开始扔掉你的Asp.net GridView吧。(更新FieldFormatorHandle参数)
- 手工实现GridView排序、删除、编辑、新增数据功能
- GridView动态绑定数据、编辑、修改、删除及分页功能
- GridView动态绑定数据、编辑、修改、删除及分页功能
- 验证GridView控件在编辑状态下输入的数据是否合法(不合法,不允许更新,同时弹出提示框)
- ASP.NET编辑与更新数据(非GridView控件实现)
- 使用jQuery实现一个类似GridView的编辑,更新,取消和删除的功能
- gridview的更新和取消的问题。网上有人问我说数据在update后还保持编辑的状态。怎么改变起状态。
- GridView动态绑定数据、编辑、修改、删除及分页功能
- GridView中按钮的编辑模式和更新数据
- GridView编辑更新时数据没有改变,总是原来的值
- ASP.NET中GridView动态绑定数据实现编辑更新
- 使用gridview实现编辑、更新、取消功能和删除功能