如何在datatable中存储已修改的网格行

时间:2013-11-19 05:25:58

标签: c# asp.net .net gridview

我有一个包含Item_idItem_valueItem_sizeItem_brand的表格,这是我的gridview的数据源,我的gridview允许用户更新列。现在我想将网格中的所有修改行存储到表中。

与提供修改记录列表的List modifiedRecords = listStore.getModifiedRecords();类似,我们是否有任何函数将整行修改后的列存储到数据表中?

2 个答案:

答案 0 :(得分:0)

我希望这也可能是更新记录的可能性。

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                   ConnectionString="<%$ Your Connection %>" 

  UpdateCommand="UPDATE [table_name] SET item_id=@item_id, item_value=@item_value,item_size=@item_size,item_brand =@item_brand WHERE(<Your Condition>)"
           >
               <UpdateParameters>
        <asp:Parameter Name=" item_id " />
        <asp:Parameter Name=" item_value " />
        <asp:Parameter Name=" item_size " />
        <asp:Parameter Name=" item_brand " />

        </UpdateParameters>
               </asp:SqlDataSource>

在c#

  protected void GridView1_RowUpdating1(object sender, GridViewUpdateEventArgs e)
{
    GridView1.Visible = true;
    ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Data Updated Succesfully')</script>");
}

答案 1 :(得分:0)

您需要使用gridview的Templetfields来使用可编辑的gridview,使用.aspx文件中的以下代码:                                         

<asp:TemplateField HeaderText="ITEM ID">
                                     <EditItemTemplate>
                                         <asp:Label ID="lblId" runat="server" Text='<%# Bind("Item_Id") %>'></asp:Label>
                                     </EditItemTemplate>
                                     <ItemTemplate>
                                         <asp:Label ID="lblId2" runat="server" Text='<%# Bind("Item_Id") %>'></asp:Label>
                                     </ItemTemplate>
                                     <HeaderStyle HorizontalAlign="Left" />
                                 </asp:TemplateField>
  <asp:TemplateField HeaderText="ITEM Value">
                                     <EditItemTemplate>
                                         <asp:TextBox ID="txtItemValue" runat="server" Text='<%# Bind("Item_Value") %>'></asp:TextBox>
                                     </EditItemTemplate>
                                     <FooterTemplate>
                                         <asp:TextBox ID="txtItemNewValue" runat="server"></asp:TextBox>
                                     </FooterTemplate>
                                     <ItemTemplate>
                                         <asp:Label ID="lblItemValue" runat="server" Text='<%# Bind("Item_Value") %>'></asp:Label>
                                     </ItemTemplate>
                                     <HeaderStyle HorizontalAlign="Left" />
                                 </asp:TemplateField>

然后首先添加一个名为DsItems的数据集链接到Item表并添加以下查询: 选择项目查询:“从TblItems中选择Item_Id,Item_Value” 在数据集中更新查询:“更新TblItems设置Item_Value = @值,其中Item_Id = @id” 现在在你的.cs文件中首先调用对DSItems的引用, 喜欢:

       using System;
       using System.Collections;
       using System.Collections.Generic;
       using System.Data;
       using System.Diagnostics;
       using System.ComponentModel;
       using YouApplication.DSItems;    //your dataset refernce




  Public Void LoadItems ()
   {
   TblItemTableAdapter item= new TblItemTableAdapter();
    DataTable items= item.GetItems();
   if (items.Rows.Count > 0) {
Gridview1.DataSource = allowances;
Gridview1.DataBind();
  }}

    protected void GridView1_RowUpdating(object sender,   System.Web.UI.WebControls.GridViewUpdateEventArgs e)
    {
        try {

    TblItemTableAdapter item = new TblItemTableAdapter();

    int item_id = txtItemId.Text;
    TextBox txtItemValue =         (TextBox)grdContact.Rows(e.RowIndex).FindControl("txtItemValue");

      item.UpdateItems(txtItemValue.text, item_id);
      Gridview1.EditIndex = -1;
    LoadItems();
} catch (Exception ex) {
    lblStatus.Text = "Items Updated.";
}
   }
       protected void GridView1_RowEditing(object sender,       System.Web.UI.WebControls.GridViewEditEventArgs e)
   {
GridView1.EditIndex = e.NewEditIndex;
LoadItems();
   }

  protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)
  {
GridView1.EditIndex = -1;
LoadItems();
   }

希望这会对你有所帮助。请随时评论进一步澄清,如果能帮到您,请不要忘记标记为答案。