none
gridview中怎样获得修改前的值和修改后的值 RRS feed

  • 问题

  • 我想把修改前的值赋给一个变量,把修改后的赋给一个变量,请问该怎样做?最好有例子给我看看!
    2009年4月8日 6:08

答案

  • 下面是一个完整的例子,这2个值不是很好控制的,最好自己来做这些功能。
    <%@ Page Language="C#" ValidateRequest="false" Debug="true" EnableViewState="True" %>
    
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    
    <script runat="server">
    
      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
      {
        GridView1.EditIndex = e.NewEditIndex;
      }
    
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
        String oldValue = "";
        String newValue = "";
        foreach (DictionaryEntry valueEntry in e.OldValues)
        {
          oldValue += valueEntry.Key + "=" + valueEntry.Value + ";<br/>";
        }
    
        foreach (DictionaryEntry valueEntry in e.NewValues)
        {
          newValue += valueEntry.Key + "=" + valueEntry.Value + ";<br/>";
        }
        Response.Write(oldValue);
        Response.Write("<hr/>");
        Response.Write(newValue);
      }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" DataKeyNames="CategoryID"
        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataSourceID="SqlDataSource1"
        AutoGenerateColumns="False">
        <Columns>
          <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
            ReadOnly="True" SortExpression="CategoryID" />
          <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
          <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"
        UpdateCommand="UPDATE [Categories] SET [CategoryName] = @CategoryName, [Description] = @Description WHERE [CategoryID] = @CategoryID"
        ConnectionString="Data Source=192.168.3.1;Initial Catalog=NorthWind;Persist Security Info=True;User ID=sa;Password=xxxxx"
        ProviderName="System.Data.SqlClient">
        <UpdateParameters>
          <asp:Parameter Name="CategoryName" Type="String" />
          <asp:Parameter Name="Description" Type="String" />
          <asp:Parameter Name="CategoryID" Type="Int32" />
        </UpdateParameters>
      </asp:SqlDataSource>
      </form>
    </body>
    </html>
    

    孟宪会
    2009年4月8日 9:03
    版主
  • 上面的例子拷贝就能执行出结果,你怎么说不行呢?你测试了吗?不行我能贴出去吗?


    说实在的,方法还是很多的,不要使用你所采用的oldvalues和newvalues。你可以放一个隐藏的文本框,也可以将作为键值处理。,下面是一种解决方法
    <%@ Page Language="C#" ValidateRequest="false" Debug="true" EnableViewState="True" %>
    
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    
    <script runat="server">
      SqlConnection conn;
      String ConnectionString = "Data Source=192.168.3.1;Initial Catalog=NorthWind;Persist Security Info=True;User ID=sa;Password=xxxx";
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
          bind();
        }
      }
      protected void bind()
      {
        string sqlstr1;
        sqlstr1 = "SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]";
        conn = new SqlConnection(ConnectionString);
        SqlDataAdapter sda2 = new SqlDataAdapter(sqlstr1, conn);
        DataSet ds2 = new DataSet();
        conn.Open();
        sda2.Fill(ds2, "e_station");
        GridView1.DataSource = ds2;
        GridView1.DataKeyNames = new string[] { "CategoryID", "CategoryName" };
        GridView1.DataBind();
        conn.Close();
    
      }
      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
      {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
      }
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
    
        GridView gv = (GridView)sender;
        for (int i = 0; i < GridView1.Columns.Count; i++)
        {
          DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
          gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
        }
        string oldTableName = "旧值:";
        string newTableName = "新值:";
        oldTableName += gv.DataKeys[e.RowIndex].Values["CategoryName"].ToString();
        newTableName += e.NewValues["CategoryName"].ToString();
        Label3.Text = oldTableName;
        Label4.Text = newTableName;
        GridView1.EditIndex = -1;
        bind();
    
      }
      protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
        GridView1.EditIndex = -1;
        bind();
      }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
        ForeColor="#333333" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
        OnRowCancelingEdit="GridView1_RowCancelingEdit" EnableViewState="true">
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <Columns>
          <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" ReadOnly="True" />
          <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
          <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" />
          <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
          <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
        </Columns>
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
      </asp:GridView>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <asp:Label ID="Label4" runat="server"></asp:Label>
      </form>
    </body>
    </html>
    
    2009年4月8日 9:55
    版主

全部回复

  • 各位老师们谁会呀,告诉我一下!
    我很着急用这个功能!

    2009年4月8日 8:47
  • 下面是一个完整的例子,这2个值不是很好控制的,最好自己来做这些功能。
    <%@ Page Language="C#" ValidateRequest="false" Debug="true" EnableViewState="True" %>
    
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    
    <script runat="server">
    
      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
      {
        GridView1.EditIndex = e.NewEditIndex;
      }
    
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
        String oldValue = "";
        String newValue = "";
        foreach (DictionaryEntry valueEntry in e.OldValues)
        {
          oldValue += valueEntry.Key + "=" + valueEntry.Value + ";<br/>";
        }
    
        foreach (DictionaryEntry valueEntry in e.NewValues)
        {
          newValue += valueEntry.Key + "=" + valueEntry.Value + ";<br/>";
        }
        Response.Write(oldValue);
        Response.Write("<hr/>");
        Response.Write(newValue);
      }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="True" DataKeyNames="CategoryID"
        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" DataSourceID="SqlDataSource1"
        AutoGenerateColumns="False">
        <Columns>
          <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" InsertVisible="False"
            ReadOnly="True" SortExpression="CategoryID" />
          <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" SortExpression="CategoryName" />
          <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
        </Columns>
      </asp:GridView>
      <asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]"
        UpdateCommand="UPDATE [Categories] SET [CategoryName] = @CategoryName, [Description] = @Description WHERE [CategoryID] = @CategoryID"
        ConnectionString="Data Source=192.168.3.1;Initial Catalog=NorthWind;Persist Security Info=True;User ID=sa;Password=xxxxx"
        ProviderName="System.Data.SqlClient">
        <UpdateParameters>
          <asp:Parameter Name="CategoryName" Type="String" />
          <asp:Parameter Name="Description" Type="String" />
          <asp:Parameter Name="CategoryID" Type="Int32" />
        </UpdateParameters>
      </asp:SqlDataSource>
      </form>
    </body>
    </html>
    

    孟宪会
    2009年4月8日 9:03
    版主
  • 不好用呀!
    还是没有值传出来!
    已经弄了两天了!一直就是没有值!
    孟老师你还有什么好的方法,可以让当用户修改e_name字段里的值的时候,同时把以该字段为表名的那个表的表明修改了!
    修改表明的存储过程你已经告诉完我了,我现在最带的问题是怎么获得我上面说的值
    2009年4月8日 9:27
  • 上面的例子拷贝就能执行出结果,你怎么说不行呢?你测试了吗?不行我能贴出去吗?


    说实在的,方法还是很多的,不要使用你所采用的oldvalues和newvalues。你可以放一个隐藏的文本框,也可以将作为键值处理。,下面是一种解决方法
    <%@ Page Language="C#" ValidateRequest="false" Debug="true" EnableViewState="True" %>
    
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    
    <script runat="server">
      SqlConnection conn;
      String ConnectionString = "Data Source=192.168.3.1;Initial Catalog=NorthWind;Persist Security Info=True;User ID=sa;Password=xxxx";
      protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
          bind();
        }
      }
      protected void bind()
      {
        string sqlstr1;
        sqlstr1 = "SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]";
        conn = new SqlConnection(ConnectionString);
        SqlDataAdapter sda2 = new SqlDataAdapter(sqlstr1, conn);
        DataSet ds2 = new DataSet();
        conn.Open();
        sda2.Fill(ds2, "e_station");
        GridView1.DataSource = ds2;
        GridView1.DataKeyNames = new string[] { "CategoryID", "CategoryName" };
        GridView1.DataBind();
        conn.Close();
    
      }
      protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
      {
        GridView1.EditIndex = e.NewEditIndex;
        bind();
      }
      protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
    
        GridView gv = (GridView)sender;
        for (int i = 0; i < GridView1.Columns.Count; i++)
        {
          DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
          gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
        }
        string oldTableName = "旧值:";
        string newTableName = "新值:";
        oldTableName += gv.DataKeys[e.RowIndex].Values["CategoryName"].ToString();
        newTableName += e.NewValues["CategoryName"].ToString();
        Label3.Text = oldTableName;
        Label4.Text = newTableName;
        GridView1.EditIndex = -1;
        bind();
    
      }
      protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
      {
        GridView1.EditIndex = -1;
        bind();
      }
    </script>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
      <title></title>
    </head>
    <body>
      <form id="form1" runat="server">
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
        ForeColor="#333333" GridLines="None" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
        OnRowCancelingEdit="GridView1_RowCancelingEdit" EnableViewState="true">
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <Columns>
          <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" ReadOnly="True" />
          <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" />
          <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" />
          <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
          <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
        </Columns>
        <RowStyle ForeColor="#000066" />
        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
      </asp:GridView>
      <asp:Label ID="Label3" runat="server"></asp:Label>
      <asp:Label ID="Label4" runat="server"></asp:Label>
      </form>
    </body>
    </html>
    
    2009年4月8日 9:55
    版主
  • 谢谢孟老师,的帮助
    DataControlFieldCell cell = gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell;
          gv.Columns[i].ExtractValuesFromCell(e.NewValues, cell, DataControlRowState.Edit, true);
    麻烦你能告诉我这两句是什么意思吗??
    2009年4月8日 10:12
  • 我是在数据绑定前把数据源(DataSet或者DataTable)保存到Session中,所以可以获得保存前的值。
    修改后的值直接遍历GridView对象就可以了。如mGridView.Rows(0).Cell(0).Value。

    2009年4月8日 11:09
  •  gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell
    等价于
    (DataControlFieldCell)gv.Rows[e.RowIndex].Cells[i]
    就是将编辑行的每个单元格转换成 DataControlFieldCell 对象。
    DataControlFieldCell 表示表格 ASP.NET 数据绑定控件(例如 DetailsView 或 GridView)的已呈现表中的单元格。

    ExtractValuesFromCell方法从当前表格单元格中提取数据控件字段的值,并将该值添加到指定的 IDictionary 集合 NewValues 中。
    孟宪会
    2009年4月8日 13:58
    版主
  • 孟老师,你好,请问如果不是用datasource 而是后台编程绑定数据源,oldvalues和newvalues字典总为空,怎么办
    2012年5月7日 7:36