积极答复者
gridview中怎样获得修改前的值和修改后的值

问题
答案
-
下面是一个完整的例子,这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>
孟宪会- 已标记为答案 KeFang Chen 2009年4月13日 3:36
-
上面的例子拷贝就能执行出结果,你怎么说不行呢?你测试了吗?不行我能贴出去吗?
说实在的,方法还是很多的,不要使用你所采用的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>
- 已标记为答案 KeFang Chen 2009年4月13日 3:36
全部回复
-
下面是一个完整的例子,这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>
孟宪会- 已标记为答案 KeFang Chen 2009年4月13日 3:36
-
上面的例子拷贝就能执行出结果,你怎么说不行呢?你测试了吗?不行我能贴出去吗?
说实在的,方法还是很多的,不要使用你所采用的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>
- 已标记为答案 KeFang Chen 2009年4月13日 3:36
-
gv.Rows[e.RowIndex].Cells[i] as DataControlFieldCell
等价于
(DataControlFieldCell)gv.Rows[e.RowIndex].Cells[i]
就是将编辑行的每个单元格转换成 DataControlFieldCell 对象。
DataControlFieldCell 表示表格 ASP.NET 数据绑定控件(例如 DetailsView 或 GridView)的已呈现表中的单元格。
ExtractValuesFromCell方法从当前表格单元格中提取数据控件字段的值,并将该值添加到指定的 IDictionary 集合 NewValues 中。
孟宪会