在 ASP.NET GridView中优雅地插入一行

分享于 

11分钟阅读

Web开发

  繁體

介绍

本文将介绍在不使用DetailsView或FormView的情况下将一行插入ASP.NET页上的GridView控件所需的方法。

使用代码

我们首先查看使用的GridView控件,

<asp:GridViewID="MyAddressesGridView"runat="server"DataKeyNames="ID"DataSourceID="MyAddressesGridViewSqlDataSource"><Columns><asp:BoundFieldHeaderText="Name"DataField="Name"/><asp:BoundFieldHeaderText="Address"DataField="Address"/><asp:BoundFieldHeaderText="City"DataField="City"/><asp:BoundFieldHeaderText="State"DataField="State"/><asp:BoundFieldHeaderText="Zip"DataField="Zip"/><asp:CommandFieldShowCancelButton="true"ShowEditButton="true"/></Columns></asp:GridView>

最简单,最快的方法是使用OnClick事件处理程序中的按钮和一些命令,这是按钮:

<asp:LinkButtonID="InsertRowLinkButton"runat="server"OnClick="InsertRowLinkButton_OnClick">Insert Row</asp:LinkButton>

步骤2 :insert命令,在的OnClick事件处理程序中,我们必须将Insert查询分配给,以下是查询的事件处理程序:

protectedvoid InsertRowLinkButton_OnClick(object sender, EventArgs e)
{
 MyAddressesGridViewSqlDataSource.InsertCommand = 
 "INSERT INTO MyAddresses (Name) VALUES (Null)";
}

步骤3:执行Insert,到目前为止,我们已经给DataConnection指定了它应该运行的命令,们没有告诉它运行命令,为此,我们调用Insert()方法DataConnectionOnClick事件处理程序的LinkButton现在如下所示:

protectedvoid InsertRowLinkButton_OnClick(object sender, EventArgs e)
{
 MyAddressesGridViewSqlDataSource.InsertCommand = 
 "INSERT INTO MyAddresses (Name) VALUES (Null)";
 MyAddressesGridViewSqlDataSource.Insert();
}

步骤4:刷新GridView数据,执行了Insert,我们更改了表中的数据,要显示新数据,必须调用DataBind()方法GridViewOnClick事件处理程序的LinkButton现在如下所示:

protectedvoid InsertRowLinkButton_OnClick(object sender, EventArgs e)
{
 MyAddressesGridViewSqlDataSource.InsertCommand = 
 "INSERT INTO MyAddresses (Name) VALUES (Null)";
 MyAddressesGridViewSqlDataSource.Insert();
 MyAddressesGridView.DataBind();
}

步骤5:设置编辑模式,现在我们已经有了GridView中的当前数据,我们必须告诉它要编辑的行,

protectedvoid InsertRowLinkButton_OnClick(object sender, EventArgs e)
{
 MyAddressesGridViewSqlDataSource.InsertCommand = 
 "INSERT INTO MyAddresses (Name) VALUES (Null)";
 MyAddressesGridViewSqlDataSource.Insert();
 MyAddressesGridView.DataBind();
 MyAddressesGridView.EditIndex = MyAddressesGridView.Rows.Count - 1;
}

为了确保位于最后一页,必须将PageIndex属性设置为最后一页GridView属性,数字可以在PageCount属性中找到GridView

protectedvoid InsertRowLinkButton_OnClick(object sender, EventArgs e)
{
 MyAddressesGridViewSqlDataSource.InsertCommand = 
 "INSERT INTO MyAddresses (Name) VALUES (Null)";
 MyAddressesGridViewSqlDataSource.Insert();
 MyAddressesGridView.DataBind();
 MyAddressesGridView.PageIndex = MyAddressesGridView.PageCount - 1;
 MyAddressesGridView.EditIndex = MyAddressesGridView.Rows.Count - 1;
} 

你注意到,我们把该命令放在设置EditIndex的命令之前,

步骤6:保存正确的数据,现在我们有一个GridView,它在编辑模式下显示新行,这本身很好,们仍然需要处理访问者,试图取消添加新行而不做更改,或者试图保存行而不输入数据,取消按钮和保存按钮。

步骤6 a:"取消"按钮。当访问者取消编辑时,调用OnRowCancelingEdit事件处理程序的GridView,看看我们更改的GridView

<asp:GridViewID="MyAddressesGridView"runat="server"DataKeyNames="ID"DataSourceID="MyAddressesGridViewSqlDataSource"OnRowCancelingEdit="MyAddressGridView_OnRowCancelingEdit"><Columns><asp:BoundFieldHeaderText="Name"DataField="Name"/><asp:BoundFieldHeaderText="Address"DataField="Address"/><asp:BoundFieldHeaderText="City"DataField="City"/><asp:BoundFieldHeaderText="State"DataField="State"/><asp:BoundFieldHeaderText="Zip"DataField="Zip"/><asp:CommandFieldShowCancelButton="true"ShowEditButton="true"/></Columns></asp:GridView>

当调用OnRowCancelingEdit事件处理程序时,我们需要知道正在编辑的行是否是我们刚刚添加的行,如果匹配,我们需要从表中删除该行,使用DeleteRow方法的GridView,然后使用GridView方法刷新DataBind中的数据,OnRowCancelingEdit事件处理程序应如下所示:

protectedvoid MyAddressGridView_OnRowCancelingEdit(object sender, 
 GridViewCancelEditEventArgs e)
{
 if (e.RowIndex.Equals(MyAddressesGridView.Rows.Count - 1))
 {
 MyAddressesGridView.DeleteRow(e.RowIndex);
 MyAddressesGridView.DataBind();
 }
}

步骤6 b:保存按钮,当访问者保存行时,首先调用OnRowUpdating事件处理程序,然后调用OnRowUpdated事件处理程序,看看我们更改的GridView

<asp:GridViewID="MyAddressesGridView"runat="server"DataKeyNames="ID"DataSourceID="MyAddressesGridViewSqlDataSource"OnRowDataBound="MyAddressesGridView_OnRowDataBound"OnRowUpdating="MyAddressGridView_OnRowUpdating"OnRowUpdated="MyAddressGridView_OnRowUpdated"OnRowCancelingEdit="MyAddressGridView_OnRowCancelingEdit"><Columns><asp:BoundFieldHeaderText="Name"DataField="Name"/><asp:BoundFieldHeaderText="Address"DataField="Address"/><asp:BoundFieldHeaderText="City"DataField="City"/><asp:BoundFieldHeaderText="State"DataField="State"/><asp:BoundFieldHeaderText="Zip"DataField="Zip"/><asp:CommandFieldShowCancelButton="true"ShowEditButton="true"/></Columns></asp:GridView>

OnRowUpdating开始,该事件处理程序在访问者保存已编辑的行时调用,并在此处实际保存数据,如果没有找到数据,我们需要从表中删除该行,如果找到数据,我们可以像平常一样保存行,OnRowUpdating事件处理程序应如下所示:

protectedvoid MyAddressGridView_OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
 string NewName = string.Empty;
 if (e.NewValues["Name"]!= null)
 {
 NewName = e.NewValues["Name"].ToString();
 }
 string NewAddress = string.Empty;
 if (e.NewValues["Address"]!= null)
 {
 NewAddress = e.NewValues["Address"].ToString();
 }
 string NewCity = string.Empty;
 if (e.NewValues["City"]!= null)
 {
 NewCity = e.NewValues["City"].ToString();
 }
 string NewState = string.Empty;
 if (e.NewValues["State"]!= null)
 {
 NewState = e.NewValues["State"].ToString();
 }
 string NewZip = string.Empty;
 if (e.NewValues["Zip"]!= null)
 {
 NewZip = e.NewValues["Zip"].ToString();
 }
 if(NewName.Equals(string.Empty) && NewAddress.Equals(string.Empty) && 
 NewCity.Equals(string.Empty) && NewState.Equals(string.Empty) && 
 NewZip.Equals(string.Empty))
 {
 e.Cancel = true;
 if (MyAddressesGridView.PageIndex == MyAddressesGridView.PageCount - 1 && 
 MyAddressesGridView.EditIndex == MyAddressesGridView.Rows.Count - 1)
 {
 MyAddressesGridView.DeleteRow(e.RowIndex);
 }
 }
 else {
 MyAddressesGridViewSqlDataSource.UpdateCommand = 
 string.Format("UPDATE MyAddresses SET Name = '{0}', Address = '{1}'," + 
 "City = '{2}', State = '{3}', Zip = '{4}' WHERE ID = {5}", 
 NewName, NewAddress, NewCity, NewState, NewZip, e.Keys[0]);
 }
}

一旦我们执行了这两个操作之一,我们需要使用GridView方法更新DataBind()中的数据,我们之前使用过这种方法,在完成所有更新后调用此事件处理程序,事件处理程序应如下所示:

protectedvoid MyAddressGridView_OnRowUpdated(object sender, GridViewUpdatedEventArgs e)
{
 MyAddressesGridView.DataBind();
}

asp  asp-net  GRID  Gridview  插入  graceful