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();
}