//数据源是自己写代码绑定的
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = DB.createConnection();
SqlCommand cmd = new SqlCommand("select experimentDepartID as 实验部门ID,edName as 实验部门名称 from experimentDepart order by experimentDepartID", conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
this.gvEdName.DataSource = sdr;
this.gvEdName.DataBind();
sdr.Close();
conn.Close();
} }
//事件代码
protected void gvEdName_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
this.gvEdName.DataKeyNames = new string[] { "experimentDepartID" };
int i = (int)this.gvEdName.DataKeys[e.RowIndex].Values[0];
SqlConnection conn = DB.createConnection();
conn.Open();
SqlCommand cmd = new SqlCommand("delete from experimentDepart where experimentDepartID='" + i + "'", conn);
SqlParameter para = new SqlParameter("@i", SqlDbType.TinyInt);
para.Value = i;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
this.gvEdName.DataBind();
conn.Close();
}
可是一点击删除按钮,就显示
“索引超出范围。必须为非负值并小于集合大小。
参数名: index ” 请各位大侠帮忙解决下,是怎么回事。
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = DB.createConnection();
SqlCommand cmd = new SqlCommand("select experimentDepartID as 实验部门ID,edName as 实验部门名称 from experimentDepart order by experimentDepartID", conn);
conn.Open();
SqlDataReader sdr = cmd.ExecuteReader();
this.gvEdName.DataSource = sdr;
this.gvEdName.DataBind();
sdr.Close();
conn.Close();
} }
//事件代码
protected void gvEdName_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
this.gvEdName.DataKeyNames = new string[] { "experimentDepartID" };
int i = (int)this.gvEdName.DataKeys[e.RowIndex].Values[0];
SqlConnection conn = DB.createConnection();
conn.Open();
SqlCommand cmd = new SqlCommand("delete from experimentDepart where experimentDepartID='" + i + "'", conn);
SqlParameter para = new SqlParameter("@i", SqlDbType.TinyInt);
para.Value = i;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
this.gvEdName.DataBind();
conn.Close();
}
可是一点击删除按钮,就显示
“索引超出范围。必须为非负值并小于集合大小。
参数名: index ” 请各位大侠帮忙解决下,是怎么回事。
解决方案 »
- 请问大家怎么只获取部份文本
- asp.net 中发布后Cookies失效
- Response.Write输出js代码用alert弹出窗口,如何使这个alert接受带有引号的变量值
- 求两个思路???
- 菜鸟请教如何把ArrayList数组转换成point数组
- 菜鸟提问:gridview要另外写什么代码才能显示吗难道
- 如何定时生成html页面??
- 怎样将当前的系统时间转换成秒?
- 怎样知道服务器的某个文件夹是否已经具有iuser_machername的权限.因为在上传的时候.如果这两个用户没有权限.就不可以!!
- 我想问一下,在Textarea中输入的字符是单字节还是双字节?
- 不用asp.net2.0自带的aspnetDB,怎么实现asp.net2.0的成员角色功能
- 急需各位帮忙,报错 选定的数据源上未找到名为xx的字段或属性。
应该这样,前提你指定了DataKeyNames 属性值为主键字段
int i = Convert.toInt32(gvEdName.DataKeys[e.RowIndex].Value);
<asp:GridView ID="gvEdName" runat="server" AutoGenerateColumns="false" DataKeyNames="experimentDepartID">protected void gvEdName_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridView gv = (GridView)sender;
int i = int.Parse(gv.DataKeys[e.RowIndex].Value.ToString());
SqlConnection conn = DB.createConnection();
conn.Open();
SqlCommand cmd = new SqlCommand("delete from experimentDepart where experimentDepartID='" + i + "'", conn);
SqlParameter para = new SqlParameter("@i", SqlDbType.TinyInt);
para.Value = i;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
this.gvEdName.DataBind();
conn.Close();
}
<asp:GridView ID="GridView1" runat="server" onrowdeleting="GridView1_RowDeleting" >
<Columns>
<asp:CommandField ButtonType="Button" HeaderText="Delete"
ShowDeleteButton="True" ShowHeader="True" />
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
using (SqlConnection conn = DB.createConnection())
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from experimentDepart where experimentDepartID=@id";
cmd.Parameters.AddWithValue("id", GridView1.DataKeys[e.RowIndex].Value);
conn.Open();
cmd.ExecuteNonQuery();
BindData(); //删完后再读取数据库数据绑定到数据库。
}
}
protected void BindData()
{
using (SqlConnection conn = DB.createConnection())
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select experimentDepartID as 实验部门ID,edName as 实验部门名称 from experimentDepart order by experimentDepartID";
conn.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
GridView1.DataSource = reader;
GridView1.DataKeyNames = new string[] { "实验部门ID" };
GridView1.DataBind();
}
}