2009/01/30
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)
{
createGridView();
}
}
private void createGridView()
{
SqlConnection con = DB.createConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from employee", con);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
GridView1.DataKeyNames = new string[] { "ID" };
GridView1.DataSource = ds.Tables["emp"];
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
createGridView();
}
protected void btnClick_Click(object sender, EventArgs e)
{
GridView1.Columns[2].Visible = false;
createGridView();
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.Columns[2].Visible = true;
createGridView();
}
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='#cccccc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
// ((LinkButton)(e.Row.Cells[4].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除吗?')");
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
if (ViewState["Order"] == null)
{
ViewState["Order"] = "ASC";
}
else
{
if (ViewState["Order"].ToString() == "ASC")
{
ViewState["Order"] = "DESC";
}
else
{
ViewState["Order"] = "ASC";
}
}
SqlConnection con = DB.createConnection();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from employee", con);
DataSet ds = new DataSet();
sda.Fill(ds, "emp");
ds.Tables["emp"].DefaultView.Sort = e.SortExpression+" "+ViewState["Order"].ToString();
GridView1.DataSource = ds.Tables["emp"].DefaultView;
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string empID = GridView1.DataKeys[e.RowIndex].Value.ToString();
SqlConnection con=DB.createConnection();
con.Open();
SqlCommand cmd=new SqlCommand("delete employee where ID='"+empID+"'",con);
cmd.ExecuteNonQuery();
con.Close();
createGridView();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
createGridView();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
createGridView();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string empID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("TextBox2")).Text;
string age = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].FindControl("TextBox1")).Text;
string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox3")).Text;
SqlConnection con = DB.createConnection();
con.Open();
SqlCommand cmd = new SqlCommand("update employee set fullName='" + name + "',age='" + age + "',address='" + address + "' where ID='" + empID + "'", con);
cmd.ExecuteNonQuery();
con.Close();
createGridView();
}
}


2009/01/30 16:59,
在 C# 中,(int)
Google Searc


