您的位置:首页 > 数据库

用 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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐