数据库
参考代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["id"] = "id";
ViewState["dir"] = "desc" ;
Bind();
}
}
/// <summary>
/// 数据绑定
/// </summary>
public void Bind()
{
string strCon = "server=.;database=studentTable;pwd=;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
string strSql = "select * from stuinfo";
SqlDataAdapter da = new SqlDataAdapter(strSql,con);
DataSet ds = new DataSet();
da.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
dv.Sort = (string)ViewState["id"] + " " + (string)ViewState["dir"];
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
con.Close();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
}
/// <summary>
/// 更新
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string strName =((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
int nSex = Convert.ToInt32(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox1")).Text.ToString());
string strSql = "update stuinfo set sex =" + nSex + ",name='" + strName + "'where id=" + this.GridView1.DataKeys[e.RowIndex].Value.ToString() + "";
string strCon = "server=.;database=studentTable;pwd=;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = strSql;
com.ExecuteNonQuery();
con.Close();
this.GridView1.EditIndex = -1;
Bind();
}
/// <summary>
/// 编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
this.GridView1.EditIndex = e.NewEditIndex;
this.Bind();
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string strSql="delete from stuinfo where id="+this.GridView1.DataKeys[e.RowIndex].Value.ToString()+"";
string strCon = "server=.;database=studentTable;pwd=;user id=sa";
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = strSql;
com.ExecuteNonQuery();
con.Close();
Bind();
}
/// <summary>
/// 取消编辑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
this.GridView1.EditIndex = -1;
this.Bind();
}
/// <summary>
/// 分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.GridView1.PageIndex = e.NewPageIndex;
this.Bind();
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string strDri = e.SortExpression;
if (ViewState["id"].ToString() == strDri)
{
if (ViewState["dir"].ToString() == "desc")
{
ViewState["dir"] = "asc";
}
else
{
ViewState["dir"] = "desc";
}
}
else
{
ViewState["id"]= e.SortExpression;
}
Bind();
}
/// <summary>
/// 绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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='yellow'");
//e.Row.Attributes.Add("onmouseover", " c=this.style.backgroundColor;this.style.backgroundColor='#223333'");
//e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
{
((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick", "return confirm ('删除吗')");
}
}
}
}