数据库是我自己写的,主键我也设置了,但是还是不行,这样吧我把代码给贴出来,麻烦纠正一下!
我先把库结构贴一下:id int 主键自动增长不为空
userName varchar(20) 不为空
sex varchar(4) 不为空
BirthDate varchar(20) 不为空
Addresss varchar(50) 不为空
Vs2008中的代码为:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//ViewState为保存服务器控件视图状态
ViewState["SortOrder"] = "id";
ViewState["OrderDire"] = "ASC";
gridviewBind();
}
}
/// <summary>
/// 绑定GridView控件
/// </summary>
private void gridviewBind()
{
SqlConnection conn = new SqlConnection("server=.;database=employees;uid=sa;pwd=sa;");
conn.Open();
string strSql = "select * from employees";
SqlDataAdapter sda = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
DataView dv = ds.Tables["emp"].DefaultView;
string sort = ViewState["SortOrder"] + " " + ViewState["OrderDire"];//定义排序表达式
dv.Sort = sort;//让dv数据表视图的排序方式为已经定义好的sort
GridView1.DataKeyNames = new string[] { "id" };
GridView1.DataSource = ds;
GridView1.DataBind();
}
/// <summary>
/// 实现翻页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gridviewBind();
}
/// <summary>
/// 实现排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sPage = e.SortExpression;//获取GridView的排序表达式并赋值给sPage这个字符变量
if (ViewState["SortOrder"].ToString()==sPage)
{
if (ViewState["OrderDire"].ToString()=="Desc")
{
ViewState["OrderDire"] = "ASC";
}
else
{
ViewState["OrderDire"] = "Desc";
}
}
else
{
ViewState["SortOrder"] = e.SortExpression;
}
gridviewBind();
}
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count-1; i++)
{
//建立模板列中的CheckBox控件的引用
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("chkCheck");
if (chkAll.Checked==true)
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
gridviewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
gridviewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string empID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string username = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
string sex = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string BirthDate = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
string Addresss = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();
string sqlStr = "update employees set userName='" + username + "',sex='" + sex + "',BirthDate='" + BirthDate + "',Addresss='" + Addresss + "' where id='" + empID + "'";
SqlConnection conn = new SqlConnection("server=.;database=employees;uid=sa;pwd=sa;");
conn.Open();
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
this.GridView1.EditIndex = -1;
gridviewBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType==DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
}
}