我GridView是用DataBind()绑定的。然后增加编辑的方式是“编辑列--CommandField--编辑,更新,取消”
前台的GridView代码是:
<asp:GridView ID="gvInfo" runat="server" Height="215px" Width="560px" OnPageIndexChanging="gvInfo_PageIndexChanging"
                                                AllowPaging="True" BackColor="White" BorderColor="#E7E7FF"   OnRowCancelingEdit="gvInfo_RowCancelingEdit"  OnRowEditing="gvInfo_RowEditing"  OnRowUpdating="gvInfo_RowUpdating"
                                                BorderWidth="1px" CellPadding="3" GridLines="Horizontal"    
                                                EnableViewState="False" BorderStyle="None">
                                                <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
                                                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                                                <Columns>
                                                    <asp:CommandField ShowEditButton="True" />
                                                </Columns>
                                                <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" 
                                                    HorizontalAlign="Center" />
                                                <SelectedRowStyle BackColor="#738A9C" ForeColor="#F7F7F7" Font-Bold="True" />
                                                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
                                                <AlternatingRowStyle BackColor="#F7F7F7" />
                                            </asp:GridView>
后台的代码为:
protected void gvInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvInfo.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void gvInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        gvInfo.EditIndex = e.RowIndex;
        
        gvInfo.EditIndex = -1;
        BindData();
    }    protected void gvInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        BindData();
        gvInfo.EditIndex = -1;
       
    }
DataBind()为:
protected void BindData()
    {
        string sqlstr = "select * from Chuzhang where 1=1 ";
        if (DropDownList2.SelectedValue != "")
        {
            sqlstr += " and 出账类型 ='" + DropDownList2.SelectedValue + "'";
        }
        if (ddlYear.SelectedValue != "")
        {
            sqlstr += " and 时间 like '" + ddlYear.SelectedValue + "%'";
        }
        if (ddlMonth.SelectedValue != "")
        {
            sqlstr += " and 时间 like '%-" + ddlMonth.SelectedValue + "-%'";
        }
        if (ddlDay.SelectedValue != "")
        {
            sqlstr += " and 时间 like '%-%-" + ddlDay.SelectedValue + "'";
        }
        if (TextBox1.Text != "")
        {
            sqlstr += " and 收款人 ='" + TextBox1.Text + "'";
        }
        if (DropDownList3.SelectedValue != "")
        {
            sqlstr += " and 银行 ='" + DropDownList3.SelectedValue + "'";
        }
        if (DropDownList1.SelectedValue != "")
        {
            sqlstr += " and 费用类型 ='" + DropDownList1.SelectedValue + "'";
        }        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds);
        gvInfo.DataSource = myds;
        gvInfo.DataBind();
        sqlcon.Close();    }结果就是我点编辑能编辑,点更新GridView却没法完成修改,点更新同时GridView不显示任何数据,我重新查询还是能显示,但是数据没做更改,求高人指点我这是哪里出的问题。(GridView显示的是我多条件查询后的数据。

解决方案 »

  1.   

    刚设置断点发现,
    protected void gvInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            gvInfo.EditIndex = e.RowIndex;
            
            gvInfo.EditIndex = -1;
            BindData();
        }
    根本就没有运行,这是为什么呢?
      

  2.   

    貌似这里面是放你更新数据的代码的呀,你都没有?就直接gvInfo.EditIndex = -1;
     
    真搞不懂你是神马意思还是把你的更新数据库代码放到这里面去进行处理
      

  3.   

    额刚复制的时候出问题了,复制错了。应该是下面这个:
      protected void gvInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            gvInfo.EditIndex = e.RowIndex;
            sqlcon = new SqlConnection(strCon);
            
            string sqlstr = "update [Chuzhang] set 出账类型='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "',时间='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',费用类型='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',银行='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',收款人='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',金额='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',备注='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "'where 编号='"
                + gvInfo.DataKeys[e.RowIndex].Value.ToString() + "'";
            SqlCommand sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();               gvInfo.EditIndex = -1;
            BindsData();
        }
    结果报错,说    :索引超出范围。必须为非负值并小于集合大小。
    参数名: index
      

  4.   

    好好看看设计器为什么不执行更新另外:
    刚设置断点发现,
    protected void gvInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    这里面没有保持数据的代码啊??表格是不会替你自动保存的,这里必须获取修改的行坐标和修改后的
    数据,自己保存
      

  5.   

    执行更新的问题解决了 在<asp:CommandField ShowEditButton="True" CausesValidation="false"/>添加CausesValidation="false"  就行了。
    现在问题却是索引超出范围。必须为非负值并小于集合大小。
    参数名: index这个问题。
      

  6.   

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
            try
    {
           string DeperMentName=((TextBox)(e.Item.FindControl( "txtDepertMent"))).Text;
           string gg=((Label)(e.Item.FindControl( "lblDepteId"))).Text; //哪一行,取所在行的ID
    string UpdateSQL = "UPDATE Tb_Department SET  DepartmentName='" + DeperMentName + "' where DepartmentId='" + gg + "'"; //连接字符串
        SqlConnection conndb=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["Exercise"]);
       conndb.Open();    SqlCommand Cmd = new SqlCommand(UpdateSQL, conndb);  
       Cmd.ExecuteNonQuery();    conndb.Close();    this.dgExercise.EditItemIndex=-1;//-1代码它不需要在编辑,取消编辑框
       DataBond();
    }
    catch (Exception a) 
    {
    Console.WriteLine("{0} First exception caught.", a); }
    }
    你要在Updating事件里面编写修改的方法才可以啊,不然你写的修改方法等同于没写
      

  7.   

    Update语句我已经写了,如5楼。就是出现了索引超出范围。必须为非负值并小于集合大小。
    参数名: index
    这个问题。DataBind()也没错啊,到底怎么回事呢。
      

  8.   

    貌似你有8个字段,而你cells[ ]索引数才为7个,当然会超出索引啦
      

  9.   

    的确是8个字段。但有一个字段是编号,是存入数据自动生成的,在SQL表里设定的。这个该怎么办啊。。
      

  10.   

    string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
    string responsible = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text.Trim();
    string other = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text.Trim();
    string sql = "update AreaName set responsible=@responsible,other=@other where id=" + id;
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.Add("@responsible",SqlDbType.VarChar);
    cmd.Parameters["@responsible"].Value = responsible;
    cmd.Parameters.Add("@other", SqlDbType.VarChar);
    cmd.Parameters["@other"].Value = other;
    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
    cmd.Dispose();
    GridView1.EditIndex = -1;
    建议你使用findcontrol来查找控件,还有我认为变量还是定义@使用SqlParameter的比较安全,, 使用‘+’连接字符串的形式虽然没错,但是有漏洞,呵呵,这只是个人喜好哈,,仅供参考
      

  11.   

    gvInfo.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() 代表是你第一个单元格的索引
      

  12.   

    楼上,你的这个方法真的很好。我值得学习。但是你现在那个方法好像是用几个TextBox让我们输入值,然后利用这些TexBox里的值来修改数据库里的内容。有什么方法能在GridView里直接修改呢?我能修改,但是却说我超出索引。该怎么改?
      

  13.   

    第一个单元格是“编号”,是无法更改的Key键。
      

  14.   

    这个就是在GridView里直接修改的呀  
    <asp:TemplateField HeaderText="负责人">
      <EditItemTemplate>
        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("responsible") %>'></asp:TextBox>
      </EditItemTemplate>
      <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("responsible") %   >'></asp:Label>
      </ItemTemplate>
    <ItemStyle Width="150px" />
    </asp:TemplateField>这是将字段转换成TemplateField只有编辑行使用的呀
      

  15.   

    索引超出范围。必须为非负值并小于集合大小。
    参数名: index还是这个问题。我前台的   DataKeyNames="编号"  也加了,DataBind()也加了,就是不知道为什么还出这问题。
    string sqlstr = "update [Chuzhang] set 出账类型='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',时间='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',费用类型='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "',银行='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + "',收款人='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[5].Controls[0])).Text.ToString().Trim() + "',金额='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString().Trim() + "',备注='"
                + ((TextBox)(gvInfo.Rows[e.RowIndex].Cells[7].Controls[0])).Text.ToString().Trim() + "'where 编号='" + gvInfo.DataKeys[e.RowIndex].Value.ToString() + "'";sqlcom = new SqlCommand(sqlstr, sqlcon);
            sqlcon.Open();
            sqlcom.ExecuteNonQuery();
            sqlcon.Close();                gvInfo.EditIndex = -1;
            BindsData();
            
      

  16.   


    点编辑的时候GridView第二列到第八列可以修改。第一列编号是键值。哪里错了啊。搞了一下午了。
      

  17.   


    this.gvInfo.Rows[rowindex].Cells[colIndex].Value 不就可以直接得到单元格的值么?
    当然colIndex最好别用index来,直接用columnName,否则要是界面需要变一下,你就有得改了
      

  18.   


    这个不太明白。colIndex是什么意思?
      

  19.   

    编号 int         Unchecked
    出账类型 varchar(50) Checked
    时间 varchar(50) Checked
    费用类型 varchar(50) Checked
    银行 varchar(50) Checked
    收款人 varchar(50) Checked
    金额 float          Checked
    备注 text          Checked数据库字段。
    我的字段没定义成英文的因为他们用这软件的不太懂英文,要求搞中文的
      

  20.   

    OK了。原来自己的DataBind()放错地方了。。