default.aspx.cs
来自「水晶报表详细资料水晶报表详细资料水晶报表详细资料」· CS 代码 · 共 97 行
CS
97 行
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 _Default : System.Web.UI.Page
{
SqlConnection myConn;
SqlDataAdapter adapter;
SqlCommand myCmd;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvBind();
}
}
protected SqlConnection GetConnectionString()
{
return (new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString()));
}
protected void gvBind()
{
try
{
myConn =GetConnectionString() ;
string strsql = "select * from tb_Student Order by 学生编号 Desc";
adapter = new SqlDataAdapter(strsql, myConn);
ds = new DataSet();
adapter.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
for(int i=0;i<this.GridView1.Rows.Count;i++)
{
DataRowView drv=ds.Tables[0].DefaultView[i];
this.GridView1.Rows[i].Cells[4].Text = Convert.ToDateTime(drv[3].ToString()).ToShortDateString();
}
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
myConn.Dispose();
adapter.Dispose();
ds.Dispose();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//鼠标移动到GridView控件的任意行时,该行自动变成指定颜色
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#BEC9F6';this.style.color='buttontext';this.style.cursor='default';");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='';this.style.color=''");
//单击删除按钮时,添加删除确认
((LinkButton)(e.Row.Cells[0].Controls[0])).Attributes.Add("onclick","return confirm('确定删除吗?')");
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
string strDeleteID = this.GridView1.Rows[e.RowIndex].Cells[1].Text;
myConn = GetConnectionString();
string strSql = "delete from tb_Student where 学生编号=" + strDeleteID;
myCmd = new SqlCommand(strSql, myConn);
myConn.Open();
myCmd.ExecuteNonQuery();
gvBind();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
myConn.Close();
myConn.Dispose();
myCmd.Dispose();
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?