注意事項:
1.直接拉近GV不用連資料庫
2.點編輯資料行->加入CommandField 下的選取,編輯更新取消,刪除
3.GridView的AllowPaging要打開(分頁用)
4.DataKeyNames 要設成 id
5.程式中的 my_test_time = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
因為沒有轉成樣板所以不能用 FindControl抓,只能用Controls[0]抓。
cell[4]是因為選取,編輯,刪除各算一個欄位還有id算一個欄位,因此要抓的test_time算第五個欄位。(從0開始算)
前端畫面:
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;
using System.Data.SqlClient;
//----自己寫的(宣告)----
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DBInit(); //---只有第一次執行本程式,才會進入 if判別式內部。
}
}
protected void DBInit() //====自己手寫的程式碼, DataAdapter / DataSet ====(Start)
{
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlDataAdapter myAdapter = new SqlDataAdapter("select id,test_time,title,author from test", Conn);
DataSet ds = new DataSet();
try
{
myAdapter.Fill(ds, "test");
GridView1.DataSource = ds;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("<HR/> Exception Error Message---- " + ex.ToString());
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox my_test_time, my_title, my_author;
//先定義三個 TextBox物件!
my_test_time = (TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0];
my_title = (TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0];
my_author = (TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0];
SqlConnection Conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString);
SqlDataAdapter myAdapter = new SqlDataAdapter();
//----------------------事先寫好 UpdateCommand / DeleteCommand / InsertCommand
myAdapter.UpdateCommand = new SqlCommand("update [test] set [test_time] = @test_time, [title] = @title, [author] = @author where [id] = @id", Conn);
//==== 上面的SQL指令,有四個參數(前面有@符號標示)。寫在下面:
//----下面 [日期格式] 如果不修正,會發生錯誤!
myAdapter.UpdateCommand.Parameters.Add("@test_time", SqlDbType.DateTime);
myAdapter.UpdateCommand.Parameters["@test_time"].Value = DateTime.Parse(my_test_time.Text);//轉換成日期
myAdapter.UpdateCommand.Parameters.Add("@title", SqlDbType.VarChar, 50);
myAdapter.UpdateCommand.Parameters["@title"].Value = my_title.Text;
myAdapter.UpdateCommand.Parameters.Add("@author", SqlDbType.VarChar, 50);
myAdapter.UpdateCommand.Parameters["@author"].Value = my_author.Text;
myAdapter.UpdateCommand.Parameters.Add("@id", SqlDbType.Int, 4);
myAdapter.UpdateCommand.Parameters["@id"].Value = (int)GridView1.DataKeys[e.RowIndex].Value;
Conn.Open();
myAdapter.UpdateCommand.ExecuteNonQuery();
myAdapter.Dispose();
GridView1.EditIndex = -1;
DBInit();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
DBInit();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
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 myAdapter = new SqlDataAdapter();
myAdapter.DeleteCommand = new SqlCommand("delete from [test] where [id] = @id", Conn);
myAdapter.DeleteCommand.Parameters.Add("@id", SqlDbType.Int, 4);
myAdapter.DeleteCommand.Parameters["@id"].Value = (int)GridView1.DataKeys[e.RowIndex].Value;
Conn.Open();
myAdapter.DeleteCommand.ExecuteNonQuery();
myAdapter.Dispose();
DBInit();
}
}
留言列表