GridViewUp.aspx.cs文件代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 gridview_GridViewUp : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridViewBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridViewBind();
}
private void GridViewBind()
{
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "SELECT *,case xueli when '1' then '博士' when '2' then '硕士' when '3' then '学士' else '' end as xueliText FROM test01 where id<1000 and id>200";
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "test01");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (((DropDownList)e.Row.FindControl("DDLXueli")) != null)
{
DropDownList ddlxueli = (DropDownList)e.Row.FindControl("DDLXueli");
// 生成 DropDownList 的值,也可以取得数据库中的数据绑定
ddlxueli.Items.Clear();
ddlxueli.Items.Add(new ListItem("博士", "1"));
ddlxueli.Items.Add(new ListItem("硕士", "2"));
ddlxueli.Items.Add(new ListItem("学士", "3"));
//
// 选中 DropDownList
ddlxueli.SelectedValue = ((HiddenField)e.Row.FindControl("HDFXueli")).Value;
//
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridViewBind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
GridViewBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string card = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBCard")).Text;
string xueli = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("DDLXueli")).SelectedValue;
string price = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBPrice")).Text;
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "update test01 set card='" + card + "',xueli='" + xueli + "',price='" + price + "' where id=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
string SqlStr = "delete from test01 where id=" + id;
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
GridViewBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
}