string colvalue=((TextBox)e.Item.Cells[i].Controls[0]).Text;
请注意,你的控件未必是Controls[0],如果你在前台的代码中有回车符或空格等符号
你要找的控件就可以变成string colvalue=((TextBox)e.Item.Cells[i].Controls[1]).Text;
建议你改成1试试

解决方案 »

  1.   

    如果无法确定的话,最好使用函数gettype(),通过返回值确定控件的类型,这个问题是经常会遇到的,多写了就会好的!!
      

  2.   

    我把0改成1了,可是出现下面的错误:
    System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。参数名:index
    楼上说的话我看不懂,我刚学c#,菜鸟一个。
      

  3.   

    楼主你编辑的时候是否每一列都有textbox存在?
    看你的情况是有没有txtbox存在的列,而你是遍历所有列,
    这时候有的列没有取到,所以出错(出了这个新的错误说明不是前面我分析的原因)
      

  4.   

    我显示出来的所有列都有textbox啊!当然我有个列就是Xdwzhh_id,我没有让它显示出来也没设置textbox,可它是最后一列阿!我的代码没有去弄最后一列的啊!因为循环条件设置了i<numCols-1
      

  5.   

    楼主把前台的DataGrid部分的代码贴出来!!
      

  6.   

    前台代码指什么?我什么html源代码么?我先贴html这个把~不知道是不是<%@ Page language="c#" Codebehind="dwzhhxx_bj.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.shiyan" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>shiyan</title>
    <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body bgColor="#6600ff" MS_POSITIONING="GridLayout">
    <form id="shiyan" method="post" runat="server">
    <FONT face="宋体">
    <asp:panel id="Panel1" style="Z-INDEX: 104; LEFT: 16px; POSITION: absolute; TOP: 86px" runat="server" BackColor="#6600FF" HorizontalAlign="Center" ForeColor="InfoText" Font-Size="X-Large" Width="500px" Height="30px">单位帐户信息 
    </asp:panel><asp:button id="Button1" style="Z-INDEX: 102; LEFT: 440px; POSITION: absolute; TOP: 62px" runat="server" Width="60px" Height="20px" BorderStyle="Outset" Text="开始查找"></asp:button><asp:textbox id="TextBox1" style="Z-INDEX: 101; LEFT: 307px; POSITION: absolute; TOP: 60px" runat="server" Width="120px" Height="20px"></asp:textbox><asp:datagrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 18px; POSITION: absolute; TOP: 126px" runat="server" BackColor="White" Width="500px" Height="150px" BorderStyle="None" CellPadding="4" BorderWidth="1px" BorderColor="#CC9966" AutoGenerateColumns="False" PageSize="5" AllowPaging="True">
    <SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
    <ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
    <HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
    <FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
    <Columns>
    <asp:BoundColumn DataField="dwdm" HeaderText="单位代码"></asp:BoundColumn>
    <asp:BoundColumn DataField="bh" HeaderText="编号"></asp:BoundColumn>
    <asp:BoundColumn DataField="mch" HeaderText="名称"></asp:BoundColumn>
    <asp:BoundColumn DataField="shbjg" HeaderText="社保机构"></asp:BoundColumn>
    <asp:BoundColumn DataField="khyh" HeaderText="开户银行"></asp:BoundColumn>
    <asp:BoundColumn DataField="yhzhh" HeaderText="银行账号"></asp:BoundColumn>
    <asp:BoundColumn DataField="frdb" HeaderText="法人代表"></asp:BoundColumn>
    <asp:BoundColumn DataField="lxr" HeaderText="联系人"></asp:BoundColumn>
    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
    <asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
    </Columns>
    <PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
    </asp:datagrid><asp:label id="Label1" style="Z-INDEX: 105; LEFT: 77px; POSITION: absolute; TOP: 61px" runat="server" Width="55px" Height="22px">请选择</asp:label><asp:dropdownlist id="DropDownList1" style="Z-INDEX: 106; LEFT: 146px; POSITION: absolute; TOP: 61px" runat="server" Width="155px" Height="20px">
    <asp:ListItem Value="1">单位代码</asp:ListItem>
    <asp:ListItem Value="2">编号</asp:ListItem>
    <asp:ListItem Value="3">名称</asp:ListItem>
    <asp:ListItem Value="4">社保机构</asp:ListItem>
    <asp:ListItem Value="5">开户银行</asp:ListItem>
    <asp:ListItem Value="6">银行账号</asp:ListItem>
    <asp:ListItem Value="7">法人代表</asp:ListItem>
    <asp:ListItem Value="8">联系人</asp:ListItem>
    </asp:dropdownlist><asp:label id="Label2" style="Z-INDEX: 107; LEFT: 82px; POSITION: absolute; TOP: 11px" runat="server" ForeColor="Red" Width="420px" Height="44px" Visible="False">Label</asp:label></FONT></form>
    </body>
    </HTML>
      

  7.   

    干脆我把所有的代码都贴出来吧
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;namespace WebApplication1
    {
    /// <summary>
    /// shiyan 的摘要说明。
    /// </summary>
    public class shiyan : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Data.SqlClient.SqlConnection sqlConnection1;
    protected System.Web.UI.WebControls.Panel Panel1;
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.Label Label2;
    protected System.Web.UI.WebControls.DropDownList DropDownList1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    if (!IsPostBack)
    BindGrid();
    } #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
    this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
    this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
    this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
    // 
    // sqlConnection1
    // 
    this.sqlConnection1.ConnectionString = "data source=localhost;password=sa;persist security info=True;user id=sa;workstati" +
    "on id=YYL-013FE796E23;packet size=4096";
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    string str1="select * from dwzhhxx where dwdm=@Id"; private void Button1_Click(object sender, System.EventArgs e)
    {
    string i=this.DropDownList1.SelectedItem.Value;
    switch (i) 
    {
    case "1":
    str1="select * from dwzhhxx where dwdm=@Id";
    break;
    case "2":
    str1="select * from dwzhhxx where bh=@Id";
    break;
    case "3":
    str1="select * from dwzhhxx where mch=@Id";
    break;
    case "4":
    str1="select * from dwzhhxx where shbjg=@Id";
    break;
    case "5":
    str1="select * from dwzhhxx where khyh=@Id";
    break;
    case "6":
    str1="select * from dwzhhxx where yhzhh=@Id";
    break;
    case "7":
    str1="select * from dwzhhxx where frdb=@Id";
    break;
    case "8":
    str1="select * from dwzhhxx where lxr=@Id";
    break;
    }
    SqlDataAdapter myCommand = new SqlDataAdapter(str1, sqlConnection1);
    myCommand.SelectCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 10));
    myCommand.SelectCommand.Parameters["@Id"].Value = this.TextBox1.Text.ToString().Trim();
    DataSet ds = new DataSet();
    myCommand.Fill(ds,"zxhh");
    DataGrid1.DataSource= ds.Tables["zxhh"].DefaultView;
    DataGrid1.DataBind();
    } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=-1;
    BindGrid();
    } private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
    BindGrid();
    } private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    String updateCmd = "update dwzhhxx set dwdm= @Xdwdm, bh= @Xbh, mch= @Xmch, shbjg= @Xshbjg, khyh=@Xkhyh; yhzhh=@Xyhzhh; frdb=@Xfrdb; lxr=@Xlxr,dwzhh_id=@Xdwzhh_id";
    SqlCommand myCommand = new SqlCommand(updateCmd,sqlConnection1); myCommand.Parameters.Add(new SqlParameter("@Xdwdm", SqlDbType.NVarChar, 10));
    myCommand.Parameters.Add(new SqlParameter("@Xbh", SqlDbType.NVarChar, 10));
    myCommand.Parameters.Add(new SqlParameter("@Xmch", SqlDbType.NVarChar, 20));
    myCommand.Parameters.Add(new SqlParameter("@Xshbjg", SqlDbType.NVarChar,10));
    myCommand.Parameters.Add(new SqlParameter("@Xkhyh", SqlDbType.NChar, 10));
    myCommand.Parameters.Add(new SqlParameter("@Xyhzhh", SqlDbType.NVarChar, 20));
    myCommand.Parameters.Add(new SqlParameter("@Xfrdb", SqlDbType.NChar, 10));
    myCommand.Parameters.Add(new SqlParameter("@Xlxr", SqlDbType.NChar, 10));
    myCommand.Parameters.Add(new SqlParameter("@Xdwzhh_id", SqlDbType.Int, 4));
    myCommand.Parameters["@Xdwzhh_id"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex]; String[] cols = {"@Xdwdm","@Xbh","@Xmch","@Xshbjg","@Xkhyh","@Xyhzhh","@Xfrdb","@Xlxr"};
        int numCols = e.Item.Cells.Count;
    for (int i=1; i<9; i++) 
    {
    string colvalue = ((TextBox)e.Item.Cells[i].Controls[0]).Text; // 检查在所需字段中是否有空值
    if (colvalue.Equals(""))
    {
    this.Label2.Visible=true;
    this.Label2.Text= "错误!修改项不允许使用空值";
    return;
    } myCommand.Parameters[cols[i-1]].Value = colvalue;
    } myCommand.Connection.Open(); try
    {
    myCommand.ExecuteNonQuery();
    this.Label2.Visible=true;
    this.Label2.Text= "已更新记录";
    DataGrid1.EditItemIndex = -1;
    }
    catch (SqlException exc)
    {
    if (exc.Number == 2627)
    {
    this.Label2.Visible=true;
    this.Label2.Text= "错误:已存在具有相同主键的记录";
    }
    else
    {
    this.Label2.Visible=true;
    this.Label2.Text= "错误:未能更新记录,请确保正确填写了字段";
    }
    } myCommand.Connection.Close(); BindGrid(); } public void BindGrid()
    {
    SqlDataAdapter myCommand = new SqlDataAdapter(str1, sqlConnection1);
    myCommand.SelectCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.NVarChar, 10));
    myCommand.SelectCommand.Parameters["@Id"].Value = this.TextBox1.Text.ToString().Trim();
    DataSet ds = new DataSet();
    myCommand.Fill(ds,"zxhh");
    DataGrid1.DataSource= ds.Tables["zxhh"].DefaultView;
    DataGrid1.DataKeyField="dwzhh_id";
    DataGrid1.DataBind();
    } private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    String deleteCmd = "delete from dwzhhxx where dwzhh_id= @Sch"; SqlCommand myCommand = new SqlCommand(deleteCmd, sqlConnection1);
    myCommand.Parameters.Add(new SqlParameter("@Sch", SqlDbType.NVarChar,11));
    myCommand.Parameters["@Sch"].Value = DataGrid1.DataKeys[(int)e.Item.ItemIndex]; myCommand.Connection.Open(); try
    {
    myCommand.ExecuteNonQuery();
    this.Label2.Visible=true;
    this.Label2.Text= "已删除记录";
    }
    catch (SqlException)
    {
    this.Label2.Visible=true;
    this.Label2.Text = "错误!未能删除记录";

    } myCommand.Connection.Close(); BindGrid(); }
    }
    }