积极答复者
GridView批量修改追问

问题
-
---- 按照演练:GridView批量修改的过程,我用NorthWind数据库一摸一样的做了一遍,没有问题。于是我依样画葫芦,对我的ReceivableTable(应收实收表)做了一个可以批量修改的GirdView表。
----
---- 与《演练》中不同的是:
----1.ReceivableTable的数据类型较多,我没有对转换为模板后的字段做长度设置;《演练》中只有nvarchar这种类型,并且还有长度设定。这个我不知道有什么影响。
----
---- 2.ReceivableTable中我除了主键我只把一部分用于测试的列转换为TextBox,而《演练》中除了主键外其他的列全部转换为TextBox。
----
在接下来的尝试中,我遇到的问题是:
----1.
// 原程序中EmployeeID 是个从1开始每次递增1的数字(主键),而我 的 PropertyIDReceivable 是个不重复但没有按顺序排序的数字(主键,int类型,但不是按1、2、3、4的顺序), 我依样画葫芦 把EmployeeID 改成了 PropertyIDReceivable ,不知道这样改是不是错了,我也不明白
originalDataTable.Select(String.Format("EmployeeID = {0}", currentID))[0] 这段代码是什么意思。。。。。。
// originalDataTable.Select(String.Format("EmployeeID = {0}", currentID))[0];
// 下面是依样画葫芦
DataRow row =
originalDataTable.Select(String.Format("PropertyIDReceivable = {0}", currentID))[0];
--------
----2.按F5测试,效果如图1,似乎一切正常;我点击“更新”键以后,奇怪的事情发生了,如图2。我仔细观察,发现:“凡是转换为模板的,不管是不是把Lable改成了TextBox,都正常运作。而没有转换为模板的列,除了主键之外,数据都被清空了(唯独第一行的数据又没有清空)”。我把那些没有转换为模板的列的ReadOnly属性都改成True,但数据又一次被清空。于是我猜想,把这些列全部转换为模板应该就不会有这些问题了,但应该有更好的办法,我估计是后台的代码写错了,我不知道错在哪里!请帮我看看。谢谢!(图3是数据库)-----------------------------------图1---------------------------------------
-----
-----------------------------------------------图2----------------------------------------
---------后台代码,注释的部分为《演练》中的原代码------------------------------------
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; public partial class ReceivableTable_ReceivableTable : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } private bool tableCopied = false; private DataTable originalDataTable; protected void GridView_Search_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) if (!tableCopied) { originalDataTable = ((DataRowView)e.Row.DataItem).Row.Table.Copy(); ViewState["originalValuesDataTable"] = originalDataTable; tableCopied = true; } } protected void UpdateButton_Click(object sender, EventArgs e) { originalDataTable = (DataTable)ViewState["originalValuesDataTable"]; foreach (GridViewRow r in GridView_Search.Rows) if (IsRowModified(r)) { GridView_Search.UpdateRow(r.RowIndex, false); } // Rebind the Grid to repopulate the original values table. tableCopied = false; GridView_Search.DataBind(); } protected bool IsRowModified(GridViewRow r) { //int currentID; //string currentTitleOfCourtesy; //string currentLastName; //string currentFirstName; //string currentTitle; //string currentExtension; int currentID; string currentReceivableID; string currentFormerOweRentTotal; string currentMonthRentReceivable01; string currentMonthRentReceivable02; string currentMonthRentReceivable03; currentID = Convert.ToInt32(GridView_Search.DataKeys[0].Value); //currentTitleOfCourtesy = ((TextBox)r.FindControl("TitleOfCourtesyTextBox")).Text; //currentLastName = ((TextBox)r.FindControl("LastNameTextBox")).Text; //currentFirstName = ((TextBox)r.FindControl("FirstNameTextBox")).Text; //currentTitle = ((TextBox)r.FindControl("TitleTextBox")).Text; //currentExtension = ((TextBox)r.FindControl("ExtensionTextBox")).Text; currentMonthRentReceivable = ((TextBox)r.FindControl("TextBox_MonthRentReceivable")).Text; currentFormerOweRentTotal = ((TextBox)r.FindControl("TextBox_FormerOweRentTotal")).Text; currentMonthRentReceivable01 = ((TextBox)r.FindControl("TextBox_MonthRentReceivable01")).Text; currentMonthRentReceivable02 = ((TextBox)r.FindControl("TextBox_MonthRentReceivable02")).Text; currentMonthRentReceivable03 = ((TextBox)r.FindControl("TextBox_MonthRentReceivable03")).Text; //DataRow row = // originalDataTable.Select(String.Format("EmployeeID = {0}", currentID))[0]; DataRow row = originalDataTable.Select(String.Format("PropertyIDReceivable = {0}", currentID))[0]; if (!currentMonthRentReceivable.Equals(row["MonthRentReceivable"].ToString())) { return true; } if (!currentFormerOweRentTotal.Equals(row["FormerOweRentTotal"].ToString())) { return true; } if (!currentMonthRentReceivable01.Equals(row["MonthRentReceivable01"].ToString())) { return true; } if (!currentMonthRentReceivable02.Equals(row["MonthRentReceivable02"].ToString())) { return true; } if (!currentMonthRentReceivable03.Equals(row["MonthRentReceivable03"].ToString())) { return true; } return false; } }
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2012年2月28日 11:04
答案
-
这样吧,我给你我写的一个例子,你自己对照着学习看看:)
批量更新GridView:http://code.msdn.microsoft.com/CSASPNETExcelLikeGridView-a88756b3
- 已标记为答案 BU XI - MSFTModerator 2012年3月6日 3:45
全部回复
-
---------------------------图3,数据库------------------------
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已标记为答案 linjiangxian11 2012年2月28日 14:27
- 取消答案标记 linjiangxian11 2012年2月28日 14:27
-
第一个问题:DataRow row =originalDataTable.Select(String.Format("PropertyIDReceivable = {0}", currentID))[0];
DataTable.Select方法,筛选出满足条件的所有行,返回DataRow数组;[0]:返回第一行。
http://msdn.microsoft.com/zh-cn/library/system.data.datatable.select(v=vs.80).aspx
-
那请问我这样依样画葫芦:
//DataRow row = // originalDataTable.Select(String.Format("EmployeeID = {0}", currentID))[0]; DataRow row = originalDataTable.Select(String.Format("PropertyIDReceivable = {0}", currentID))[0];
蒙的对不对呢?原程序中EmployeeID 是个从1开始每次递增1的数字(主键),而我 的 PropertyIDReceivable 是个不重复但没有按顺序排序的数字(主键,int类型,但不是按1、2、3、4的顺序)。
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
我参考了您提供的http://msdn.microsoft.com/zh-cn/library/system.data.datatable.select(v=vs.80).aspx
还是不理解("PropertyIDReceivable = {0}", currentID)这一段是什么意思!
--
我用Response查看了 currentID ,发现它的值都是‘1’,因为我的GridView选择了分页,并且每页只有10行,所以Response.write(currentID)得到的是1111111111
--
Response.write(row)则得到" .datarow"(.前面我不记得了),如果点击“更新”键,除了第一行的的数据能正常保留外,其他9行都被清空了!我彻底郁闷了。。。
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
这样吧,我给你我写的一个例子,你自己对照着学习看看:)
批量更新GridView:http://code.msdn.microsoft.com/CSASPNETExcelLikeGridView-a88756b3
- 已标记为答案 BU XI - MSFTModerator 2012年3月6日 3:45