Download presentation
Presentation is loading. Please wait.
Published byMark Hodge Modified over 8 years ago
1
ADO.NET
2
ADO.NET 精靈化設計 精靈化設計 程式化設計 程式化設計 SQL 指令 SQL 指令 DataGrid, DataList DataGrid, DataList DataBind DataBind
3
精靈化設計 工具物件 工具物件 SqlConnectionSqlConnection SqlCommandSqlCommand SqlAdapterSqlAdapter DataGrid DataGrid DataSourceDataSource DataBindDataBind 自動化格式 自動化格式 屬性產生器 屬性產生器
4
DataGrid Step 1: 在版面配置一個 DataGrid
5
DataGrid Step 2: 設定資料庫連線
6
DataGrid
7
DataGrid
8
DataGrid
9
DataGrid
10
DataGrid
11
DataGrid
12
DataGrid
13
DataGrid
14
DataGrid
15
DataGrid Step 3: 產生資料集 DataSet
16
DataGrid
17
DataGrid
18
DataGrid Step 4: 設定 DataGrid 的 DataSource
19
DataGrid
20
DataGrid Step 5: 設定 DataGrid 的 DataBind
21
DataGrid
22
美化 DataGrid
23
使用 DataGrid 進行資料異動
24
屬性產生器
25
排序 private void DataGrid1_ SortCommand (object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees ORDER BY " + e.SortExpression + " DESC ",sqlConnection1); da.Fill(ds12, ” employees ” ); DataGrid1.DataBind(); } 要設定 DataGrid 的 AllowSorting 只能排序一次,使用 Hidden 控制項改成連續排序
26
完整排序 private void DataGrid1_ SortCommand (object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees ORDER BY " + e.SortExpression + " " + Hidden1.Value.ToString(),sqlConnection1); da.Fill(ds1, ” employees ” ); if(Hidden1.Value=="desc") { Hidden1.Value="asc"; }else{ Hidden1.Value="desc"; } DataGrid1.DataBind(); } 注意 IsPostBack
27
模組化 showdata private void showdata() { SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees",sqlConnection1); da.Fill(ds12, ” employees ” ); DataGrid1.DataBind(); }
28
分頁
29
分頁 private void showdata() { SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees",sqlConnection1); da.Fill(ds12, ” employees ” ); DataGrid1.DataBind(); } private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid1.CurrentPageIndex=e.NewPageIndex; showdata(); }
30
選取資料 I
31
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { Response.Write(e.Item.Cells[0].Text); Response.Write(DataGrid1.DataKeys[e.Item.ItemIndex]); showdata(); } 要設定 DataKeyField 屬性
32
選取資料 I ( 子母表單 ) private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { switch(e.CommandName.ToString()) { case "Select": SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees where employeeID=" + DataGrid1.DataKeys[e.Item.ItemIndex],sqlConnection1); DataSet ds2=new DataSet(); da.Fill(ds2, ” employees ” ); DataGrid2.DataSource=ds2; DataGrid2.DataBind(); break; }
33
選取資料 II 加入樣板 編輯樣板 要將 AutoPostBack 設定成 True
34
選取資料 II 加入樣板
35
選取資料 II 不像 DataGrid 有預設對應的事件,需要自行設定 初始化 CheckBox 事件 ( 寫在 Page_Load 事件中 ) for(int i=0;i<=DataGrid1.Items.Count-1;i++) { ((CheckBox)DataGrid1.Items[i].Cells[5].Controls[1]).Checke dChanged+= new EventHandler(selecteditem); } 事件名稱
36
選取資料 II private void selecteditem(object sender, System.EventArgs e) { string str; str="1=0"; for(int i=0;i<=DataGrid1.Items.Count-1;i++) { if(((CheckBox)DataGrid1.Items[i].Cells[5].Controls[1]).Checked) { str+=" or employeeID=" + DataGrid1.Items[i].Cells[0].Text; } SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM Orders where " + str,sqlConnection1); DataSet ds2=new DataSet(); da.Fill(ds2, ” company ” ); DataGrid2.DataSource=ds2; DataGrid2.DataBind(); }
37
編輯
38
編輯 private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.EditItemIndex=e.Item.ItemIndex; showdata(); } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.EditItemIndex=-1; showdata(); }
39
編輯 private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { SqlDataAdapter da=new SqlDataAdapter("update employees set LastName='" + ((TextBox)e.Item.Cells[1].Controls[0]).Text + "' where employeeID='" + DataGrid1.DataKeys[e.Item.ItemIndex] + "'",sqlConnection1); da.Fill(ds1, ” employees ” ); DataGrid1.EditItemIndex=-1; showdata(); } 注意! PK 值不應該可以更改
40
新增 SqlDataAdapter da=new SqlDataAdapter("insert into employees (pwd) values ('" + TextBox1.Text + "')",sqlConnection1); da.Fill(dataset12, ” employees ” ); showdata(); 可以配合 Panel 物件
41
刪除
42
刪除 private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { SqlDataAdapter da=new SqlDataAdapter("delete from employees where id='" + DataGrid1.DataKeys[e.Item.ItemIndex] + "'",sqlConnection1); da.Fill(dataset12, ” employees ” ); if(e.Item.ItemIndex==0) // 判斷是不是那頁最後一筆 { if(DataGrid1.CurrentPageIndex !=0) { DataGrid1.CurrentPageIndex=DataGrid1.CurrentPageIndex-1; } showdata(); }
43
刪除 + Confirm 視窗 private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { if(e.Item.DataItem==null) return; string msg="javascript:return confirm(' 確定要刪除 " + DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim() + "?')"; ((Button)e.Item.Cells[4].Controls[0]).Attributes["onclick"]=msg; } 需將 ” 刪除 ” 選項改成按紐形式
44
顯示單筆資料
45
顯示單筆資料 static int row_pos; private void Button2_Click(object sender, System.EventArgs e) { showdata(); row_pos++; if(row_pos > dataSet21.Tables["employees"].Rows.Count-1) { row_pos=dataSet21.Tables["employees"].Rows.Count-1; } showdetail(); Response.Write(row_pos); } private void showdetail() { TextBox2.Text=dataSet21.Tables["Employees"].Rows[row_pos]["Emplo yeeid"].ToString(); TextBox3.Text=dataSet21.Tables["Employees"].Rows[row_pos]["fIRSTN ame"].ToString(); }
46
搜尋資料
47
搜尋資料 // SEARCH SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM customers where customerid LIKE'" + TextBox3.Text + "%'",sqlConnection1); da.Fill(dataSet12,"customers"); DataGrid1.DataBind();
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.