DataGrid multi-headed head (transferred from www.codeproject.com)

xiaoxiao2021-03-06  39

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 Test {///

/// WebForm3 summary description. /// public class WebForm3: System.Web.UI.Page {protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1; 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.Web.UI.WebControls.DataGrid DataGrid1; protected System.Data.SqlClient.SqlConnection sqlConnection1; protected System.Web.UI.WebControls. DataGrid DataGrid2; private ASPNetDatagridDecorator m_add = new ASPNetDatagridDecorator (); private void Page_Load (object sender, System.EventArgs e) {if {this.bind_Data () (this.IsPostBack!); this.bind_data ();} // here Place the user code to initialize the page} public void bind_data () {dataable dt = new datatable (); this.sqldataadapter1.fill (dt); this.dataGrid2.datasource = DT;

THIS.DATABIND ();

} Public void bind_Data () {TableCell cell = null; m_add.DatagridToDecorate = this.DataGrid1; ArrayList header = new ArrayList (); cell = new TableCell (); cell.Text = "Code"; cell.RowSpan = 2; cell . Horizontalalign = horizontalalign.center; header.add (cell);

Cell = new Tablecell (); cell.text = "name"; // cell.rowspan = 1; cell.horizontalalign = horizontalalign.center; header.add (Cell);

Cell = new Tablecell (); cell.text = "name"; cell.columnspan = 3; cell.horizontalalign = horizontalalign.center; header.add (cell); cell = new TableCell (); cell.text = "agnge Cell.RowSpan = 2; cell.horizontalalign = horizontalalign.center; header.add (cell);

Cell = new Tablecell (); cell.text = "School"; cell.columnspan = 3; cell.horizontalalign = horizontalalign.center; header.add (cell);

Cell = new Tablecell (); cell.text = "Religion"; cell.rowspan = 2; cell.horizontalalign = horizontalalign.center; header.add (cell);

m_add.addmergeheader; Header;

// this.DataGrid1.datasource = getdata (); // this.dataGrid1.database (); datatable dt = new data (); this.sqldataadapter1.fill (dt); this.dataGrid1.datasource = DT;

THIS.DATABIND ();

}

#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} ///

/// Designer Supports the required method - Do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter (); this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand (); this.sqlInsertCommand1 = new System. Data.SqlClient.SqlCommand (); this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand (); this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand (); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection (); // // 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", "Authors", new system.data.common.Datacolumnmapping [] {new system.data.common.datacolumnMapping ("a U_ID "," au_id "), new system.data.common.Datacolumnmapping (" au_lname "," au_lname "), new system.data.common.Datacolumnmapping (" au_fname "," au_fname "), new system.data.common .Datacolumnmapping ("Phone", "Phone"), New System.Data.common.DatacolumnMapping ("Address", "Address"), New System.Data.common.DatacolumnMapping ("City", "City"

), New system.data.common.datacolumnmapping ("State", "State"), new system.data.common.DatacolumnMapping ("zip", "zip"), new system.data.common.DatacolumnMapping ("contract" , "contract")})}); this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1; // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT au_id, au_lname, au_fname, phone, address, city, state, zip , contract FROM " " authors "; this.sqlSelectCommand1.Connection = this.sqlConnection1; // // sqlInsertCommand1 // this.sqlInsertCommand1.CommandText = @" INSERT INTO authors (au_id, au_lname, au_fname, phone, address, city, State, zip, contract) VALUES (@au_id, @au_lname, @Au_fname, @phone, @address, @city, @State, @zip, @contract; select au_id, au_lname, au_fname, phone, address, city, state , ZIP, Contract from Authors Where (Au_ID = @Au_id) "; this.sqlInsertCommand1.Connection = this.sqlConnection1; this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter (" @ au_id ", System.Data.SqlDbType.VarChar, 11," au_id ")); this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter (" @ au_lname ", System.Data.SqlDbType.VarChar, 40," au_lname ")); this.sqlInsertCommand1.Parameters.Add ( New system.data.sqlclient.sqlparameter ("@ au_fname", system.data.sqldbtype.varchar, 20, "au_fname"));

this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ phone", System.Data.SqlDbType.VarChar, 12, "phone")); this.sqlInsertCommand1.Parameters.Add (new System.Data .SqlClient.SqlParameter ( "@ address", System.Data.SqlDbType.VarChar, 40, "address")); this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ city", System. Data.sqldbtype.varchar, 20, "city"); this.sqlinsertcommand1.parameters.add (new system.data.sqlclient.sqlparameter ("@ State", System.Data.sqldbtype.varchar, 2, "state") ); this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ zip", System.Data.SqlDbType.VarChar, 5, "zip")); this.sqlInsertCommand1.Parameters.Add (new System .Data.SqlClient.SqlParameter ( "@ contract", System.Data.SqlDbType.Bit, 1, "contract")); // // sqlUpdateCommand1 // this.sqlUpdateCommand1.CommandText = @ "UPDATE authors SET au_id = @au_id , au_lname = @au_lname, au_fname = @AU_FNAME, Phone = @Phone, Address = @address, City = @City, state = @state, zip = @zip, contract = @contract WHERE (au_id = @Original_au_id) AND (address = @Original_address OR @Original_address IS NULL AND address IS NULL) AND (au_fname = @Original_au_fname) AND ( au_lname = @Original_au_lname) AND (city = @Original_city OR @Original_city IS NULL AND city IS NULL) AND (contract = @Original_contract) AND (phone = @Original_phone) AND (state = @Original_state OR @

Original_state IS NULL AND state IS NULL) AND (zip = @Original_zip OR @Original_zip IS NULL AND zip IS NULL); SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contract FROM authors WHERE (au_id = @ au_id) "; this.sqlUpdateCommand1.Connection = this.sqlConnection1; this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter (" @ au_id ", System.Data.SqlDbType.VarChar, 11," au_id ") ); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ au_lname", System.Data.SqlDbType.VarChar, 40, "au_lname")); this.sqlUpdateCommand1.Parameters.Add (new System .Data.SqlClient.SqlParameter ( "@ au_fname", System.Data.SqlDbType.VarChar, 20, "au_fname")); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ phone", System.Data.SqlDbType.VarChar, 12, "phone")); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ address", System.Data.SqlDbType.VarChar, 40, "address ")); This.sqlupdateCommand1. Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ city", System.Data.SqlDbType.VarChar, 20, "city")); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ("@state", system.data.sqldbtype.varchar, 2, "state"); this.sqlupdateCommand1.Parameters.add (new system.data.sqlclient.sqlparameter ("@ zip", system.data.sqldbtype. VARCHAR, 5, "ZIP")); this.sqlupdateCommand1.Parameters.add (new system.data.sqlclient.sqlparameter ("@ contract", system.data.sqldbtype.bit, 1, "contract"));

this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_au_id", System.Data.SqlDbType.VarChar, 11, System.Data.ParameterDirection.Input, false, ((System.Byte) (0 ), (System.byte) (0)), "au_id", system.data.datarowversion.original, null); this.sqlupdateCommand1.Parameters.add (new system.data.sqlclient.sqlparameter) ", System.data.sqldbtype.varchar, 40, system.data.parameterDirection.input, false, ((System.byte) ((System.byte) (0))," Address ", System. Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_au_fname", System.Data.SqlDbType.VarChar, 20, System.Data.ParameterDirection.Input, False, ((System.byte) ((System.byte) (0)), "au_fname", system.data.datarowversion.original, null); this.sqlupdateCommand1.parameters.Add (New System) .Data.sqlclient.sqlparameter ("@ original_au_lname", System.Data.sqldbtype.varchar, 40, system.data.parameterDirection.input, FA LSE, ((System.byte) ((System.byte) (0)), "AU_LNAME", System.Data.DataRowVersion.ORIGINAL, NULL); this.sqlupdateCommand1.Parameters.Add (New System) .Data.sqlclient.sqlparameter ("@ Original_City", System.Data.sqldbtype.varchar, 20, System.Data.ParameterDirection.Input, False, ((System.byte)) ((System.byte) 0)), "city", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_contract", System.Data.SqlDbType.Bit, 1, System.Data.ParameterDirection.Input, False, ((System.byte) ((System.byte) (0)), "Contract"

, System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_phone", System.Data.SqlDbType.VarChar, 12, System.Data.ParameterDirection .NPUT, FALSE ((System.byte) ((System.byte) (0)), "Phone", System.Data.DataRowVersion.original, NULL); this.sqlupdateCommand1.Parameters.Add (New System.data.sqlcl CLIENT.SQLPARAMETER ("@ Original_State", System.Data.Sqldbtype.varchar, 2, System.Data.ParameterDirection.Input, False, ((System.byte)) (((System. byte) (0)), "state", System.Data.DataRowVersion.Original, null)); this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_zip", System.Data.SqlDbType .Varchar, 5, system.data.parameterDirection.input, false (0)), (System.byte) (0)), "ZIP", System.Data.DataRowVersion.original, NULL ))); // sqldeleteCommand1 // this.sqldeleteCommand1.commandtext = @ "delete from authors where (au_id = @original_au_id) and (address = @Original_address OR @Original_address IS NULL AND address IS NULL) AND (au_fname = @Original_au_fname) AND (au_lname = @Original_au_lname) AND (city = @Original_city OR @Original_city IS NULL AND city IS NULL) AND (contract = @Original_contract) AND (phone = @Original_phone) AND (state = @Original_state OR @Original_state IS NULL AND state IS NULL) AND (zip = @Original_zip OR @Original_zip IS NULL AND zip IS NULL) "; this.sqlDeleteCommand1.Connection = this.sqlConnection1; this.sqldeleteCommand1.Parameters.add (new system.data.sqlclient.sqlparameter)

Original_au_id ", System.Data.sqldbtype.varchar, 11, System.Data.ParameterDirection.Input, False, ((System.byte) (0))," Au_ID ", System .Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_address", System.Data.SqlDbType.VarChar, 40, System.Data.ParameterDirection.Input , False, ((System.byte) ((System.byte) (0)), "Address", System.Data.DataRowVersion.original, Null); this.sqldeleteCommand1.Parameters.Add (New System.data.sqlclient.sqlparameter ("@ Original_au_fname", System.Data.sqldbtype.varchar, 20, System.Data.ParameterDirection.Input, False, ((System.byte)), ((System.byte) (0)), "au_fname", System.Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_au_lname", System.Data.SqlDbType.VarChar , 40, System.Data.ParameterDirection.input, False, ((System.byte) ((System.byte) (0)), "Au_LName", System.Data.DataRowver sion.Original, null)); this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_city", System.Data.SqlDbType.VarChar, 20, System.Data.ParameterDirection.Input, false, (System.byte) ((System.byte) (0)), "city", system.data.datarowversion.original, null); this.sqldeleteCommand1.Parameters.Add (New System.Data . Sqlclient.sqlparameter ("@ Original_Contract", System.Data.SqldbType.bit, 1, System.Data.ParameterDirection.Input, False, ((System.byte)) ((System.byte) (0) ), "contract", system.data.datarowversion.original, null);

this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_phone", System.Data.SqlDbType.VarChar, 12, 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) ", System.data.sqldbtype.varchar, 2, system.data.parameterDirection.input, false, ((System.byte) ((System.byte) (0))," State ", System. Data.DataRowVersion.Original, null)); this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ( "@ Original_zip", System.Data.SqlDbType.VarChar, 5, System.Data.ParameterDirection.Input, FALSE, ((System.byte) (0)), ((System.byte) (0)), "ZIP", System.Data.DataRowVersion.original, Null); // // SqlConnection1 // this.sqlConnection1 .Connectionstring = "Workstation id = gw; packet size = 4096; user ID = sa; data source = gw; persist security inf" "o = false; Initial Catalog = PUBS" ; This.Load = new system.eventhandler (this.page_load);

} #Endregion}} using System; using System.Web.UI.WebControls; using System.Collections; using System.Web.UI;. Namespace Test {///

/// Summary description for ASPNetDatagridDecorator /// < / summary> public class ASPNetDatagridDecorator {public ASPNetDatagridDecorator () {// // TODO: Add constructor logic here //} private ASPNetDatagridDecorator (DataGrid DatagridToDecorate, ArrayList HeaderCells) {// // TODO: Add constructor logic here // this.DatagridToDecorate = DatagridToDecorate; AddMergeHeader (HeaderCells);} public void AddMergeHeader (ArrayList arrHeaderCells) {m_arrHeaderCells = arrHeaderCells;} private void NewRenderMethod (HtmlTextWriter writer, Control ctl) {int iCurrIndex = 0; for (int i = 0; i 1) {iCurrIndex = item.ColumnSpan-1;} if (item.RowSpan> 1) {m_htblRowspanIndex.Add (iCurrIndex i , iCurrinde x i);} item.renderControl (Writer);} Writer.WriteEndtag ("TR"); // *** Add the style attributes That Was Defined in Design Time // To Our Second Row So They Both Will Have The THE same appearance m_dgDatagridToDecorate.HeaderStyle.AddAttributesToRender (writer); // *** Insert the second row writer.RenderBeginTag ( "TR"); // *** Render all the cells that was defined in design time, except the last one / / BECAUSE WE Already Rendered IT Above for (INT I = 0; I

Tr> Close Tag Because The Writer Will Do Tag Because :)} ///

/// Gets or sets the datagrid to decorate /// < / summary> public DataGrid DatagridToDecorate {get {return m_dgDatagridToDecorate;} set {if (! null = m_dgDatagridToDecorate) {m_dgDatagridToDecorate.ItemCreated - = new DataGridItemEventHandler (DatagridToDecorate_ItemCreated);} m_dgDatagridToDecorate = value; m_dgDatagridToDecorate.ItemCreated = new DataGridItemEventHandler (DatagridToDecorate_ItemCreated); }} private void DatagridToDecorate_ItemCreated (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {// *** Examine if the item created is the header item ListItemType lit = e.Item.ItemType; if (ListItemType.Header = = LIT) {// *** redirect the default header rendering method to outremeth dendelegate (New RenderMethod);}} /// /// Hold The R eference to the datagrid to decorate /// private DataGrid m_dgDatagridToDecorate = null; private ArrayList m_arrHeaderCells = null; private Hashtable m_htblRowspanIndex = new Hashtable ();

}

Transferred from

http://www.codeproject.com/aspnet/mergeDataGridHeader.asp

转载请注明原文地址:https://www.9cbs.com/read-76450.html

New Post(0)