读书人

gridview控件中删除列时出现System.

发布时间: 2012-02-06 15:52:45 作者: rapoo

gridview控件中删除列时,出现System.ArgumentOutOfRangeException
这是报的错误((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text = 'GridView1.Rows[e.RowIndex].Cells[0].Controls[0]' threw an exception of type 'System.ArgumentOutOfRangeException'
HTML代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
   
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing1" OnRowUpdating="GridView1_RowUpdating"
Style="z-index: 100; left: 122px; position: absolute; top: 88px">
<Columns>
<asp:BoundField DataField="EmployeeID" HeaderText="1" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="Address" HeaderText="Address" />
<asp:BoundField DataField="HomePhone" HeaderText="HomePhone" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>



C#代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
LoadData();
}
}
private void LoadData()
{
SqlDataAdapter sqlDa = new SqlDataAdapter();
SqlConnection conn = new SqlConnection("Server=(local);Integrated Security=SSPI;Database=Northwind");
SqlCommand selectCmd = new SqlCommand("select EmployeeID,LastName,FirstName,Title,Address,HomePhone from Employees");
selectCmd.Connection = conn;
conn.Open();
sqlDa.SelectCommand = selectCmd;
DataSet ds = new DataSet();
sqlDa.Fill(ds, "Employees");
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
conn.Close();
conn.Dispose();
sqlDa.Dispose();
}

protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;


LoadData();

}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
LoadData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection conn = new SqlConnection("Server=(local);Integrated Security=SSPI;Database=Northwind");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
string employeeID = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
string lastName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string firstName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string title = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string homePhone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
cmd.CommandText = "update Employees set Title='" + title + "',Address='" + address + "',FirstName='" + firstName + "',LastName='" + lastName + "',HomePhone='" + homePhone + "' where EmployeeID='" + employeeID + "'";
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
conn.Close();
conn.Dispose();
cmd.Dispose();
GridView1.EditIndex = -1;
LoadData();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection("Server=(local);Integrated Security=SSPI;Database=Northwind");
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
conn.Open();
string employeeID = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
cmd.CommandText = "delete from Employees where EmplyeeID=" + employeeID;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
conn.Close();
conn.Dispose();
cmd.Dispose();
GridView1.EditIndex = -1;
LoadData();
}
}



现在的问题是更新是没有问题的,但是遇到删除的时候就开始报异常,问题出在红色的一行,郁闷死了,同样的句子,在更新里面就可以用,在删除里就出现问题,请达人指点!

[解决办法]
在GridView2中加一属性 DataKeyNames="EmplyeeID"
取employeeID 使用下面的方法
string employeeID = this.GridView2.DataKeys[e.RowIndex].Value.ToString();
[解决办法]
((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;

不知道是不是 Controls[0] 的 更新和除面示是不一的,更新有textbox

除可以直接用 cells[0].Text

读书人网 >asp.net

热点推荐