更新网格数据时出错 [英] Error in updating the grid data

查看:127
本文介绍了更新网格数据时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更新网格值时收到一个名为Index Out of Range的错误。请查看以下代码。

  protected   void  GridView1_RowUpdating( object  sender,GridViewUpdateEventArgs e)
{

string username = GridView1.DataKeys [e.RowIndex] .Values [ 名称] .ToString(); // 索引超出范围。必须是非负且小于集合的大小参数名称:index(这是错误)
TextBox备注=(TextBox)GridView1.Rows [e.RowIndex] .FindControl( txtRemarks);
string connStr = ConfigurationManager.ConnectionStrings [ myConnectionString]的ConnectionString。
SqlConnection con = new SqlConnection(connStr);

con.Open();
SqlCommand cmd = new SqlCommand( update gridsample set备注=' + Remarks.Text + '其中Name =' + username + ';,con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
BindEmployeeDetails();
}
}

解决方案

试试这个。



 <   asp:GridView     ID   =  GridView1     runat   =  server    DataKeyNames   = 名称   >  
< / asp:GridView >





指定

 DataKeyNames =  名称 





并尝试得到这样的用户名

  string  username = GridView1.DataKeys [e.RowIndex] .Value.ToString(); 


I am getting an error called Index Out of Range while updating the grid value. Kindly have a look at the following code.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            
            string username = GridView1.DataKeys[e.RowIndex].Values["Name"].ToString();//Index was out of range. Must be non-negative and less than the size of the collection Parameter name: index (this is error)
            TextBox Remarks = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtRemarks");
            string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(connStr);
            
            con.Open();
            SqlCommand cmd = new SqlCommand("update gridsample set Remarks='" + Remarks.Text + "'where Name='" + username+"';", con);
            cmd.ExecuteNonQuery();
            con.Close();
            GridView1.EditIndex = -1;
            BindEmployeeDetails();
        }
    }

解决方案

try this.

<asp:GridView ID="GridView1" runat="server" DataKeyNames="Name" >
   </asp:GridView>



Specifie the

DataKeyNames="Name"



and try to get username like this

string username = GridView1.DataKeys[e.RowIndex].Value.ToString();


这篇关于更新网格数据时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆