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;
using System.Configuration;
namespace DataGridTest
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg;
public int lastIndex=0;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
dgBind();
}
}
SqlConnection conn=new SqlConnection(ConfigurationSettings.AppSettings["SqlConn"].ToString());
public void dgBind()
{
string sql="select employeeid,lastname,birthdate,city from employees";
SqlDataAdapter da=new SqlDataAdapter(sql,conn);
DataSet ds=new DataSet();
da.Fill(ds);
dg.DataSource=ds;
dg.DataBind();
}
private void dg_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int id=(int)dg.DataKeys[e.Item.ItemIndex];
string sql="delete employees where employeeid="+id.ToString();
SqlCommand comm=new SqlCommand(sql,conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
lastIndex=dg.CurrentPageIndex;
if((dg.PageCount-dg.CurrentPageIndex==1)&&dg.Items.Count==1)
{
if(dg.PageCount>1)
{
lastIndex-=1;
}
else
{
lastIndex=0;
}
}
dg.CurrentPageIndex=lastIndex;
dgBind();
}
private void dg_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dg.CurrentPageIndex=e.NewPageIndex;
dgBind();
}
private void dg_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
LinkButton lb=(LinkButton)e.Item.Cells[3].Controls[0];
lb.Attributes.Add("onclick","return confirm('你确实要删除吗')");
}
}
private void dg_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dg.EditItemIndex=e.Item.ItemIndex;
dgBind();
}
private void dg_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dg.EditItemIndex=-1;
dgBind();
}
private void dg_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int id=(int)dg.DataKeys[e.Item.ItemIndex];
TextBox city=(TextBox)e.Item.Cells[2].FindControl("txtCity");
string sql="update employees set city='"+city.Text+"' where employeeid="+id;
SqlCommand comm=new SqlCommand(sql,conn);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
dg.EditItemIndex=-1;
dgBind();
}
}
}
索引超出范围。必须为非负值并小于集合大小。参数名: index
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentOutOfRangeException: 索引超出范围。必须为非负值并小于集合大小。参数名: index
源错误:
行 67: private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
行 68: {
行 69: int id=(int)DataGrid1.DataKeys[e.Item.ItemIndex];
行 70: string sql="delete test1 where uid='"+id+"'";
行 71: SqlCommand comm=new SqlCommand(sql,conn);
还出现了这么一个错误,这是怎么回事呀
这位大哥你是在那上班还上学,怎么你发的代码和我前几天写的一样呀!!
就连里面的变量的名称都取得一样,难道这就是[缘分]!!
太让人不可思议了,
下面的代码是相关处理函数不是完整的代码,能完成查询删除修改功能:(希望能给你一定的帮助)
public class DataGrid : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button btndelete;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!this.IsPostBack)
{
this.DataBindToDataGrid();
}
// 在此处放置用户代码以初始化页面
}
private void DataBindToDataGrid() //数据绑定
{
SqlConnection con=DB.createCon();
SqlDataAdapter sdr=new SqlDataAdapter();
sdr.SelectCommand=new SqlCommand("select userName,userIdentity,userPwd,sex,brith,province,city,educational,pay from member",con);
con.Open();
DataSet ds=new DataSet();
sdr.Fill(ds,"member");
this.DataGrid1.DataKeyField="userName";
this.DataGrid1.DataSource=ds.Tables["member"];
this.DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
this.DataBindToDataGrid();
}
private void DataGrid1_ItemDataBound(object sender,System.Web.UI.WebControls.DataGridItemEventArgse) //为数据里添加相应的客户端处理方法
{
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
((LinkButton)(e.Item.Cells[9].Controls[0])).Attributes.Add("onclick","return confirm('确认删除吗?');");
}
}
private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) //排序处理
{
//客户端预处理
if(ViewState["Order"]==null)
{
ViewState["Order"]="ASC";
}
else
{
if(ViewState["Order"].ToString()=="ASC")
{
ViewState["Order"]="DESC";
}
else
{
ViewState["Order"]="ASC";
}
}
//数据联接
SqlConnection con=DB.createCon(); //数据库请使用自己的连接,不能照搬下面的代码
SqlDataAdapter sdr=new SqlDataAdapter();
sdr.SelectCommand=new SqlCommand("select userName,userIdentity,userPwd,sex,brith,province,city,educational,pay from member",con);
con.Open();
DataSet ds=new DataSet();
sdr.Fill(ds,"member");
ds.Tables["member"].DefaultView.Sort=e.SortExpression+" "+ViewState["Order"].ToString();
this.DataGrid1.DataSource=ds.Tables["member"].DefaultView;
this.DataGrid1.DataBind();
}
private void DataGrid1_DeleteCommand(objectsource,System.Web.UI.WebControls.DataGridCommandEventArgs e) //删除处理函数
{
string userName=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection con=DB.createCon();
SqlCommand cmd=new SqlCommand("delete from member where userName='"+userName+"'",con);
con.Open();
cmd.ExecuteNonQuery();
this.DataBindToDataGrid();
}
private void DataGrid1_EditCommand(objectsource,System.Web.UI.WebControls.DataGridCommandEventArgs e) //修改处理函数
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
this.DataBindToDataGrid();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
this.DataBindToDataGrid();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) //数据更新处理函数
{
string userNameID=this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
string userIdentity=((TextBox)(e.Item.Cells[1].Controls[0])).Text;
string userPwd=((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string sex=((TextBox)(e.Item.Cells[3].Controls[0])).Text;
string brith=((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string province=((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string city=((TextBox)(e.Item.Cells[6].Controls[0])).Text;
string educational=((TextBox)(e.Item.Cells[7].Controls[0])).Text;
string pay=((TextBox)(e.Item.Cells[8].Controls[0])).Text;
SqlConnection con=DB.createCon();
SqlCommand cmd=new SqlCommand("update member set userIdentity='"+userIdentity+"',userPwd='"+userPwd+"',sex='"+sex+"',brith='"+brith+"',province='"+province+"',city='"+city+"',educational='"+educational+"',pay='"+pay+"' where userName='"+userNameID+"'",con);
con.Open();
cmd.ExecuteNonQuery();
this.DataGrid1.EditItemIndex=-1;
this.DataBindToDataGrid();
}