實現自定義分頁(如:改變傳統datagrid的分頁顯示、通過A-Z的字母來分頁等)、選擇的check


代碼
using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace DataGridDemoCS { /// /// Demo1 的摘要說明。 /// public class DemoFinal : System.Web.UI.Page { protected System.Data.SqlClient.SqlConnection sqlConnection1; protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; //注意:dataset在編輯狀態中將清空,所以可以將dataset申明爲static,這樣就可以保持dataset狀態; //靜態成員變量不可以用this來調用 protected DataGridDemoCS.dsCustomers dsCustomers1; protected DataGridDemoCS.dsCustomers.CustomersDataTable dsCustomerTable; protected DataGridDemoCS.dsCustomers.CustomersRow dsCustomerRow; protected System.Data.SqlClient.SqlCommand sqlSelectCommand1; protected System.Data.SqlClient.SqlCommand sqlInsertCommand1; protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1; protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1; protected System.Data.SqlClient.SqlConnection sqlConnection2; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Button BtnInsertOneRow; protected System.Web.UI.WebControls.Button BtnDeleteGrouply; protected System.Web.UI.WebControls.Panel PnlPage; protected System.Data.DataView dv; protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter2; protected System.Data.SqlClient.SqlConnection sqlConnection3; protected System.Web.UI.WebControls.DataGrid DataGrid1; public int ExpandedItem=7; public Unit HostColumnWidth; private DataGrid detailsGrid; const string ExpandText = "+"; protected System.Data.SqlClient.SqlCommand sqlSelectCommand2; protected System.Data.SqlClient.SqlCommand sqlInsertCommand2; protected System.Data.SqlClient.SqlCommand sqlUpdateCommand2; protected System.Data.SqlClient.SqlCommand sqlDeleteCommand2; protected System.Web.UI.WebControls.Button Button2; const string CollapseText = "-"; private void Page_Load(object sender, System.EventArgs e) { // 在此處放置用戶代碼以初始化頁面 if (!Page.IsPostBack) { ExpandedItem = -1; testWorkFlow=""; ViewState["Sort"]="CustomerID"; ViewState["orderby"]="asc"; bindDate(); BtnDeleteGrouply.Attributes.Add("OnClick","javascript:DeleteGrouply();"); HostColumnWidth = Unit.Pixel(150); } } void bindDate() { testWorkFlow=""; sqlDataAdapter1.Fill(dsCustomers1,"Customers"); if ((string)(ViewState["Sort"])!="") dv.Sort=(string)ViewState["Sort"]+" "+ViewState["orderby"]; else dsCustomers1.Customers.DefaultView.Sort="CustomerID"; if ((string)(ViewState["alpha"])!="") { dv.RowFilter="CustomerID LIKE '"+(string)(ViewState["alpha"])+"%'"; } DataGrid1.DataBind(); m_CreatePageTimes=0; } #region Web 窗體設計器生成的代碼 override protected void OnInit(EventArgs e) { // // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。 // InitializeComponent(); base.OnInit(e); } /// /// 設計器支持所需的方法 - 不要使用代碼編輯器修改 /// 此方法的內容。 /// private void InitializeComponent() { this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter(); this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlConnection2 = new System.Data.SqlClient.SqlConnection(); this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand(); this.dsCustomers1 = new DataGridDemoCS.dsCustomers(); this.dv = new System.Data.DataView(); this.sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter(); this.sqlDeleteCommand2 = new System.Data.SqlClient.SqlCommand(); this.sqlConnection3 = new System.Data.SqlClient.SqlConnection(); this.sqlInsertCommand2 = new System.Data.SqlClient.SqlCommand(); this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand(); this.sqlUpdateCommand2 = new System.Data.SqlClient.SqlCommand(); ((System.ComponentModel.ISupportInitialize)(this.dsCustomers1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dv)).BeginInit(); this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated); this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand); this.DataGrid1.Init += new System.EventHandler(this.DataGrid1_Init); this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged); this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand); this.DataGrid1.PreRender += new System.EventHandler(this.DataGrid1_PreRender); this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand); this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand); this.DataGrid1.DataBinding += new System.EventHandler(this.DataGrid1_DataBinding); this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand); this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand); this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound); this.Button1.Click += new System.EventHandler(this.Button1_Click); this.BtnInsertOneRow.Click += new System.EventHandler(this.BtnInsertOneRow_Click); this.BtnDeleteGrouply.Click += new System.EventHandler(this.BtnDeleteGrouply_Click); // // sqlConnection1 // this.sqlConnection1.ConnectionString = "workstation id=/"FANCY-FAMILY/";packet size=4096;user id=sa;data source=/"FANCY-FAMI" + "LY/";persist security info=False;initial catalog=Northwind"; // // sqlDataAdapter1 // this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1; this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1; this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1; this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "Customers", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID"), new System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"), new System.Data.Common.DataColumnMapping("ContactName", "ContactName"), new System.Data.Common.DataColumnMapping("ContactTitle", "ContactTitle"), new System.Data.Common.DataColumnMapping("Address", "Address"), new System.Data.Common.DataColumnMapping("City", "City"), new System.Data.Common.DataColumnMapping("Region", "Region"), new System.Data.Common.DataColumnMapping("PostalCode", "PostalCode"), new System.Data.Common.DataColumnMapping("Country", "Country"), new System.Data.Common.DataColumnMapping("Phone", "Phone"), new System.Data.Common.DataColumnMapping("Fax", "Fax")})}); this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1; // // sqlDeleteCommand1 // this.sqlDeleteCommand1.CommandText = @"DELETE FROM Customers WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL)"; this.sqlDeleteCommand1.Connection = this.sqlConnection2; this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null)); // // sqlConnection2 // this.sqlConnection2.ConnectionString = "workstation id=/"FANCY-FAMILY/";packet size=4096;user id=sa;data source=/"FANCY-FAMI" + "LY/";persist security info=False;initial catalog=Northwind"; // // sqlInsertCommand1 // this.sqlInsertCommand1.CommandText = @"INSERT INTO Customers(CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)"; this.sqlInsertCommand1.Connection = this.sqlConnection2; this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone")); this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax")); // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region," + " PostalCode, Country, Phone, Fax FROM Customers"; this.sqlSelectCommand1.Connection = this.sqlConnection2; // // sqlUpdateCommand1 // this.sqlUpdateCommand1.CommandText = @"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName = @ContactName, ContactTitle = @ContactTitle, Address = @Address, City = @City, Region = @Region, PostalCode = @PostalCode, Country = @Country, Phone = @Phone, Fax = @Fax WHERE (CustomerID = @Original_CustomerID) AND (Address = @Original_Address OR @Original_Address IS NULL AND Address IS NULL) AND (City = @Original_City OR @Original_City IS NULL AND City IS NULL) AND (CompanyName = @Original_CompanyName) AND (ContactName = @Original_ContactName OR @Original_ContactName IS NULL AND ContactName IS NULL) AND (ContactTitle = @Original_ContactTitle OR @Original_ContactTitle IS NULL AND ContactTitle IS NULL) AND (Country = @Original_Country OR @Original_Country IS NULL AND Country IS NULL) AND (Fax = @Original_Fax OR @Original_Fax IS NULL AND Fax IS NULL) AND (Phone = @Original_Phone OR @Original_Phone IS NULL AND Phone IS NULL) AND (PostalCode = @Original_PostalCode OR @Original_PostalCode IS NULL AND PostalCode IS NULL) AND (Region = @Original_Region OR @Original_Region IS NULL AND Region IS NULL); SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax FROM Customers WHERE (CustomerID = @CustomerID)"; this.sqlUpdateCommand1.Connection = this.sqlConnection2; this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CompanyName", System.Data.SqlDbType.NVarChar, 40, "CompanyName")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactName", System.Data.SqlDbType.NVarChar, 30, "ContactName")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ContactTitle", System.Data.SqlDbType.NVarChar, 30, "ContactTitle")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Address", System.Data.SqlDbType.NVarChar, 60, "Address")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@City", System.Data.SqlDbType.NVarChar, 15, "City")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Region", System.Data.SqlDbType.NVarChar, 15, "Region")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PostalCode", System.Data.SqlDbType.NVarChar, 10, "PostalCode")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Country", System.Data.SqlDbType.NVarChar, 15, "Country")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Phone", System.Data.SqlDbType.NVarChar, 24, "Phone")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Fax", System.Data.SqlDbType.NVarChar, 24, "Fax")); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Address", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Address", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_City", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CompanyName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CompanyName", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactName", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactName", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ContactTitle", System.Data.SqlDbType.NVarChar, 30, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ContactTitle", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Country", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Country", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Fax", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Fax", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Phone", System.Data.SqlDbType.NVarChar, 24, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_PostalCode", System.Data.SqlDbType.NVarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "PostalCode", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Region", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "Region", System.Data.DataRowVersion.Original, null)); // // dsCustomers1 // this.dsCustomers1.DataSetName = "dsCustomers"; this.dsCustomers1.Locale = new System.Globalization.CultureInfo("zh-CN"); // // dv // this.dv.Table = this.dsCustomers1.Customers; // // sqlDataAdapter2 // this.sqlDataAdapter2.DeleteCommand = this.sqlDeleteCommand2; this.sqlDataAdapter2.InsertCommand = this.sqlInsertCommand2; this.sqlDataAdapter2.SelectCommand = this.sqlSelectCommand2; this.sqlDataAdapter2.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "Orders", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("OrderID", "OrderID"), new System.Data.Common.DataColumnMapping("OrderDate", "OrderDate"), new System.Data.Common.DataColumnMapping("ShipName", "ShipName"), new System.Data.Common.DataColumnMapping("ShipAddress", "ShipAddress"), new System.Data.Common.DataColumnMapping("ShipCity", "ShipCity"), new System.Data.Common.DataColumnMapping("CustomerID", "CustomerID")})}); this.sqlDataAdapter2.UpdateCommand = this.sqlUpdateCommand2; // // sqlDeleteCommand2 // this.sqlDeleteCommand2.CommandText = @"DELETE FROM Orders WHERE (OrderID = @Original_OrderID) AND (CustomerID = @Original_CustomerID OR @Original_CustomerID IS NULL AND CustomerID IS NULL) AND (OrderDate = @Original_OrderDate OR @Original_OrderDate IS NULL AND OrderDate IS NULL) AND (ShipAddress = @Original_ShipAddress OR @Original_ShipAddress IS NULL AND ShipAddress IS NULL) AND (ShipCity = @Original_ShipCity OR @Original_ShipCity IS NULL AND ShipCity IS NULL) AND (ShipName = @Original_ShipName OR @Original_ShipName IS NULL AND ShipName IS NULL)"; this.sqlDeleteCommand2.Connection = this.sqlConnection3; this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_OrderID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "OrderID", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_OrderDate", System.Data.SqlDbType.DateTime, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "OrderDate", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ShipAddress", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipAddress", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ShipCity", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipCity", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ShipName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipName", System.Data.DataRowVersion.Original, null)); // // sqlConnection3 // this.sqlConnection3.ConnectionString = "workstation id=/"FANCY-FAMILY/";packet size=4096;user id=sa;data source=/"FANCY-FAMI" + "LY/";persist security info=False;initial catalog=Northwind"; // // sqlInsertCommand2 // this.sqlInsertCommand2.CommandText = @"INSERT INTO Orders(OrderDate, ShipName, ShipAddress, ShipCity, CustomerID) VALUES (@OrderDate, @ShipName, @ShipAddress, @ShipCity, @CustomerID); SELECT OrderID, OrderDate, ShipName, ShipAddress, ShipCity, CustomerID FROM Orders WHERE (OrderID = @@IDENTITY)"; this.sqlInsertCommand2.Connection = this.sqlConnection3; this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OrderDate", System.Data.SqlDbType.DateTime, 8, "OrderDate")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ShipName", System.Data.SqlDbType.NVarChar, 40, "ShipName")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ShipAddress", System.Data.SqlDbType.NVarChar, 60, "ShipAddress")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ShipCity", System.Data.SqlDbType.NVarChar, 15, "ShipCity")); this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); // // sqlSelectCommand2 // this.sqlSelectCommand2.CommandText = "SELECT OrderID, OrderDate, ShipName, ShipAddress, ShipCity, CustomerID FROM Order" + "s"; this.sqlSelectCommand2.Connection = this.sqlConnection3; // // sqlUpdateCommand2 // this.sqlUpdateCommand2.CommandText = @"UPDATE Orders SET OrderDate = @OrderDate, ShipName = @ShipName, ShipAddress = @ShipAddress, ShipCity = @ShipCity, CustomerID = @CustomerID WHERE (OrderID = @Original_OrderID) AND (CustomerID = @Original_CustomerID OR @Original_CustomerID IS NULL AND CustomerID IS NULL) AND (OrderDate = @Original_OrderDate OR @Original_OrderDate IS NULL AND OrderDate IS NULL) AND (ShipAddress = @Original_ShipAddress OR @Original_ShipAddress IS NULL AND ShipAddress IS NULL) AND (ShipCity = @Original_ShipCity OR @Original_ShipCity IS NULL AND ShipCity IS NULL) AND (ShipName = @Original_ShipName OR @Original_ShipName IS NULL AND ShipName IS NULL); SELECT OrderID, OrderDate, ShipName, ShipAddress, ShipCity, CustomerID FROM Orders WHERE (OrderID = @OrderID)"; this.sqlUpdateCommand2.Connection = this.sqlConnection3; this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OrderDate", System.Data.SqlDbType.DateTime, 8, "OrderDate")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ShipName", System.Data.SqlDbType.NVarChar, 40, "ShipName")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ShipAddress", System.Data.SqlDbType.NVarChar, 60, "ShipAddress")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ShipCity", System.Data.SqlDbType.NVarChar, 15, "ShipCity")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerID", System.Data.SqlDbType.NVarChar, 5, "CustomerID")); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_OrderID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "OrderID", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_CustomerID", System.Data.SqlDbType.NVarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerID", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_OrderDate", System.Data.SqlDbType.DateTime, 8, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "OrderDate", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ShipAddress", System.Data.SqlDbType.NVarChar, 60, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipAddress", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ShipCity", System.Data.SqlDbType.NVarChar, 15, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipCity", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_ShipName", System.Data.SqlDbType.NVarChar, 40, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "ShipName", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OrderID", System.Data.SqlDbType.Int, 4, "OrderID")); this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Load += new System.EventHandler(this.Page_Load); ((System.ComponentModel.ISupportInitialize)(this.dsCustomers1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dv)).EndInit(); } #endregion private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { testWorkFlow=""; DataGrid1.EditItemIndex=e.Item.ItemIndex; bindDate(); } private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { DataGrid1.EditItemIndex=-1; bindDate(); } private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { //注意:e.Item.Cells[0].Text ;是引用不到任何值的,即每次的狀態改變都將引發該狀態下的數據,不可以跨越狀態的獲取 //現在是處於編輯狀態,所以只能通過類型轉換來獲取textbox裏的值 //((TextBox)e.Item.Cells[1].Controls[0]).Text //CustomerID設置爲只讀,在編輯狀態下,仍然處於label狀態,只能用e.Item.Cells[0].Text;來獲取單元格中的值 string strCustomerID=e.Item.Cells[1].Text; string strCompanyName=((TextBox)e.Item.Cells[2].Controls[0]).Text ; string strContactName=((TextBox)e.Item.Cells[3].Controls[0]).Text ; string strCity=((TextBox)e.Item.Cells[4].Controls[0]).Text ; //在編輯狀態調用模版列有兩種方式: //1)((TextBox)e.Item.Cells[4].Controls[1]).Text; 注意必須是Control[1]不是[0] control[0]是一個{System.Web.UI.LiteralControl}, Text: "/r/n/t/t/t/t/t/t/t/t" //2)((TextBox)e.Item.FindControl["控件id"]).Text; FindControl可以查找該單元內的任何一個控件id string strCoutry=((TextBox)e.Item.Cells[5].Controls[1]).Text ; string strPhone=((TextBox)e.Item.Cells[6].Controls[0]).Text ; sqlDataAdapter1.Fill(dsCustomers1,"Customers"); dsCustomerRow=dsCustomers1.Customers.FindByCustomerID(strCustomerID); dsCustomerRow.CompanyName=strCompanyName; dsCustomerRow.ContactName=strContactName; dsCustomerRow.City=strCity; dsCustomerRow.Country=strCoutry; dsCustomerRow.Phone=strPhone; sqlDataAdapter1.Update(dsCustomers1,"Customers"); dsCustomers1.AcceptChanges(); DataGrid1.EditItemIndex=-1; bindDate(); //e.Item.ItemIndex } private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { string CustomersID=e.Item.Cells[0].Text; } private void DataGrid1_DataBinding(object sender, System.EventArgs e) { Response.Write(e.ToString()); } private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) { if (e.CommandName=="alpha") { ViewState["alpha"]=e.CommandArgument; m_CreatePageTimes=0; DataGrid1.CurrentPageIndex=0; bindDate(); } if (e.CommandName=="Cancle") { DataGrid1.ShowFooter=false; } if (e.CommandName=="Insert") { sqlDataAdapter1.Fill(dsCustomers1); dsCustomerRow=(dsCustomers.CustomersRow)dsCustomers1.Customers.NewRow(); dsCustomerRow.CustomerID=((TextBox)e.Item.Cells[1].Controls[0]).Text ; dsCustomerRow.CompanyName=((TextBox)e.Item.Cells[2].Controls[0]).Text; dsCustomerRow.ContactName =((TextBox)e.Item.Cells[3].Controls[0]).Text; dsCustomerRow.City=((TextBox)e.Item.Cells[4].Controls[0]).Text; dsCustomerRow.Country=((TextBox)e.Item.Cells[5].Controls[0]).Text; dsCustomerRow.Phone=((TextBox)e.Item.Cells[6].Controls[0]).Text; dsCustomers1.Customers.AddCustomersRow(dsCustomerRow); sqlDataAdapter1.Update(dsCustomers1,"Customers"); dsCustomers1.AcceptChanges(); DataBind(); DataGrid1.ShowFooter=false; } if (e.CommandName=="Expand") { ExpandItem(e.Item); } } private void ExpandItem(DataGridItem item) { if (item.ItemIndex == (ExpandedItem % DataGrid1.PageSize)) SetExpandedItem(item, false); else SetExpandedItem(item, true); } // Adjust the index of the expanded item private void SetExpandedItem(DataGridItem item, bool expand) { if (expand) ExpandedItem = (DataGrid1.PageSize*DataGrid1.CurrentPageIndex+item.ItemIndex); else ExpandedItem = -1; } private void DataGrid1_PreRender(object sender, System.EventArgs e) { } public string testWorkFlow=""; private int m_CreatePageTimes = 0; private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { testWorkFlow+=e.Item.ItemType.ToString()+"->"; if (e.Item.ItemType==ListItemType.Header) { //排序 foreach(TableCell c in e.Item.Cells) { if (c.Controls.Count >0 && c.Controls[0].GetType().ToString()=="System.Web.UI.WebControls.DataGridLinkButton") { if (((LinkButton)c.Controls[0]).Text==(string)ViewState["Sort"]) { Label l=new Label(); l.Font.Name="Webdings"; if ((string)ViewState["orderby"]=="asc") l.Text="5"; else l.Text="6"; c.Controls.Add(l); } } } } if (e.Item.ItemType==ListItemType.Footer) { int i=0;LinkButton lb; foreach(TableCell c in e.Item.Cells) { if (i==0) c.Controls.Add(new CheckBox()); else if (i==e.Item.Cells.Count-2) { lb=new LinkButton(); lb.Text="插入"; lb.CommandName="Insert"; c.Controls.Add(lb); } else if (i==e.Item.Cells.Count-1) { lb=new LinkButton(); lb.Text="取消"; lb.CommandName="Cancle"; c.Controls.Add(lb); } else c.Controls.Add(new TextBox()); i++; } } if (e.Item.ItemType==ListItemType.Pager) { if (m_CreatePageTimes==0) { e.Item.Cells[0].Controls.Clear(); LinkButton l; for (int i=65;i<65+25;i++) { l=new LinkButton(); LiteralControl lc=new LiteralControl(); lc.Text=" "; l.CommandName="alpha"; l.CommandArgument=Convert.ToChar(i).ToString(); l.Text=Convert.ToChar(i).ToString(); e.Item.Cells[0].Controls.Add(l); e.Item.Cells[0].Controls.Add(lc); } l=new LinkButton(); l.Text="All"; l.CommandName="alpha"; l.CommandArgument=""; e.Item.Cells[0].Controls.Add(l); m_CreatePageTimes ++; } else if(m_CreatePageTimes==1) { foreach(Control ct in e.Item.Cells[0].Controls) { switch(ct.GetType().ToString()) { case "System.Web.UI.WebControls.Label": { Label l=(Label)ct; l.Text="當前頁["+l.Text+"]"; break; } case "System.Web.UI.LiteralControl": { LiteralControl lc=(LiteralControl)ct; lc.Text="|"; break; } case "System.Web.UI.WebControls.DataGridLinkButton": { LinkButton lb=(LinkButton)ct; if (e.Item.Cells[0].Controls.IndexOf(ct)==0 && lb.Text=="...") lb.Text="[向前翻頁]"; else if(e.Item.Cells[0].Controls.IndexOf(ct)==e.Item.Cells[0].Controls.Count-1 && lb.Text=="...") lb.Text="[向後翻頁]"; else lb.Text="第["+lb.Text+"]頁"; break; } } } } } } private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) { DataGrid1.CurrentPageIndex=e.NewPageIndex; m_CreatePageTimes=0; bindDate(); } private void Button1_Click(object sender, System.EventArgs e) { Response.Write(testWorkFlow+"End"); } private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { ViewState["Sort"]=e.SortExpression; if ((string)ViewState["orderby"]=="asc") ViewState["orderby"]="desc"; else ViewState["orderby"]="asc"; m_CreatePageTimes=0; bindDate(); } private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) { } public void Check_Changed(object sender,System.EventArgs e) { CheckBox chk=(CheckBox)sender; DataGridItem dgi; dgi=(DataGridItem)chk.Parent.Parent; if (chk.Checked) { dgi.BackColor = DataGrid1.SelectedItemStyle.BackColor; dgi.ForeColor = DataGrid1.SelectedItemStyle.ForeColor; } else { dgi.BackColor = DataGrid1.ItemStyle.BackColor; dgi.ForeColor = DataGrid1.ItemStyle.ForeColor; } } private void BtnInsertOneRow_Click(object sender, System.EventArgs e) { DataGrid1.ShowFooter=true; } private void BtnDeleteGrouply_Click(object sender, System.EventArgs e) { sqlDataAdapter1.Fill(dsCustomers1,"Customers"); foreach (DataGridItem dgi in DataGrid1.Items) { CheckBox chk=(CheckBox)dgi.Cells[0].Controls[1]; if (chk.Checked==true) { string CustomerID=dgi.Cells[1].Text; dsCustomerRow=dsCustomers1.Customers.FindByCustomerID(CustomerID); dsCustomerRow.Delete(); } } sqlDataAdapter1.Update(dsCustomers1); dsCustomers1.AcceptChanges(); } private void Button2_Click(object sender, System.EventArgs e) { string CustomerID=""; foreach (DataGridItem dgi in DataGrid1.Items) { if (((CheckBox)dgi.Cells[0].FindControl("chkOne")).Checked==true) CustomerID+=dgi.Cells[1].Text; } Response.Write(CustomerID); } } }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章