using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class d9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dbInit();
}

}

private void dbInit()
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand= new SqlCommand("select id,title,author,test_time from test",conn);
DataSet ds = new DataSet();
try
{
da.Fill(ds, "test");
GridView1.DataSource = ds.Tables["test"].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{

Response.Write("錯誤訊息"+ex.ToString());
}
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{

}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
dbInit();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
dbInit();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter();
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.SelectCommand = new SqlCommand("select * from test", conn);
DataSet ds = new DataSet();
da.FillSchema(ds,SchemaType.Source,"test");
da.Fill(ds,"test");
DataRow currentRow = ds.Tables["test"].Rows.Find(GridView1.DataKeys[e.RowIndex].Value);
currentRow.Delete();
da.Update(ds,"test");
dbInit();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
dbInit();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//先取得編輯模式中,textBox的值
TextBox my_title, my_author, my_test_time;
my_title = GridView1.Rows[e.RowIndex].Cells[4].Controls[0] as TextBox;
my_author = GridView1.Rows[e.RowIndex].Cells[5].Controls[0] as TextBox;
my_test_time = GridView1.Rows[e.RowIndex].Cells[6].Controls[0] as TextBox;

//建立連線數據
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);

//建立adapter物件以便連結實體資料庫與填入記憶體資料庫dataset
SqlDataAdapter da= new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from test", conn);
DataSet ds = new DataSet();

//建立可以操作 dataset T-sql語法的命令的物件
SqlCommandBuilder cb = new SqlCommandBuilder(da);

//adapter將資料表test的架構表傳給dataset,以便了解誰是pk及使用datatable相同的指令操作
da.FillSchema(ds,SchemaType.Source,"test");

//adapter將資料表test的內容傳給dataset
da.Fill(ds, "test");

//取得目前所在列
DataRow currentRow = ds.Tables["test"].Rows.Find(GridView1.DataKeys[e.RowIndex].Value);
//將目前列的內容置換
currentRow["title"] = my_title.Text;
currentRow["author"] = my_author.Text;
currentRow["test_time"] = DateTime.Now;

//更新實體資料庫內容
da.Update(ds,"test");

//退出編輯模式
GridView1.EditIndex = -1;

//重新綁定資料給gridview1
dbInit();
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button myBtn = e.Row.Cells[2].FindControl("Button1") as Button;
myBtn.Attributes.Add("onclick", "return confirm('您確定要刪除資料嗎?')");
}
}
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ylw1125 的頭像
    ylw1125

    程式搜集分享精靈

    ylw1125 發表在 痞客邦 留言(0) 人氣()