4 ROC 2012年7月19日 我的代码如下 protected void OnRowDeleting(object sender,GridViewDeleteEventArgs e) { int index = Convert.ToInt32(e.RowIndex) ; DataTable dt = ViewState [dt] as DataTable; dt.Rows [index] .Delete(); ViewState [ dt] = dt; BindGrid(); } protected void BindGrid() { GridView1.DataSource = ViewState [dt]为DataTable; GridView1.DataBind(); } 当我删除行模式中gridview的第4行显示错误如下 对象引用未设置为对象的实例。 以上错误在下面的行显示如下 dt.Rows [index] .Delete(); 请帮帮我。我的abvoe代码中有什么问题。I am deleting partiuclar row in gridview. Run mode as follows Waitlistid Course Batchdate 1 AFF 25 Aug 2014 2 MFA 26 Sep 2015 3 ERS 20 Mar 2012 4 ROC 19 Jul 2012 My code as follows protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e) { int index = Convert.ToInt32(e.RowIndex); DataTable dt = ViewState["dt"] as DataTable; dt.Rows[index].Delete(); ViewState["dt"] = dt; BindGrid(); } protected void BindGrid() { GridView1.DataSource = ViewState["dt"] as DataTable; GridView1.DataBind(); } When i delete the 4th row in gridview in row mode shows error as follows Object reference not set to an instance of an object. The above error shows in below line as follows dt.Rows[index].Delete(); please help me. what is the problem in my abvoe code.推荐答案//see the reference below://Single row delete http://csharp-video-tutorials.blogspot.in/2013/03/deleting-data-from- gridview-using.html [ ^ ]http://csharp-video-tutorials.blogspot.in/2013/03/deleting-data-from-gridview-using.html[^]//multiple rows delete http://csharp-video-tutorials.blogspot.in/2013/03/delete-multiple-rows-from-aspnet.html [ ^ ]异常的原因对象引用未设置为对象的实例。是你的dt应为null。 始终检查dt不为空并在删除前有行。将代码更改为如下所示,因此如果条件满足则删除成功,并且在没有数据的情况下不会抛出异常。The reason for the exception "Object reference not set to an instance of an object." is your dt should be null.Always check dt is not null and has rows before deleting. Change your code to something like below, so delete is success if the conditions are met and no exception is thrown where there is no data.DataTable dt = ViewState["dt"] as DataTable;if(dt != null && dt.Rows.Count > 0){ dt.Rows[index].Delete();} 我希望这可以让您了解代码中的错误并帮助解决它。I hope this gives an idea of what is wrong in your code and helps solve it.尝试to dt.Rows.RemoveAt(index)try to dt.Rows.RemoveAt(index) 这篇关于使用带有c#的asp.net删除gridview中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-23 23:37