datagird 多表头(转自www.codeproject.com)

dotnet 专栏收录该内容
4 篇文章 0 订阅

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
{
 /// <summary>
 /// WebForm3 的摘要说明。
 /// </summary>
 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.IsPostBack)
   {
    this.bind_Data();
    this.bind_data();
   }
   
   // 在此处放置用户代码以初始化页面
  }
  public void bind_data()
  {
   DataTable 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 = "Age";
   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.DataBind();
   
   
   DataTable dt=new DataTable();
   this.sqlDataAdapter1.Fill(dt);
   this.DataGrid1.DataSource=dt;

    this.DataBind ();


  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  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("au_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("@Original_address", System.Data.SqlDbType.VarChar, 40, 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_au_fname", System.Data.SqlDbType.VarChar, 20, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((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, false, ((System.Byte)(0)), ((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)(0)), ((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)(0)), ((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.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "phone", System.Data.DataRowVersion.Original, null));
   this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_state", System.Data.SqlDbType.VarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((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, ((System.Byte)(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)), ((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)(0)), ((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)(0)), ((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)(0)), ((System.Byte)(0)), "au_lname", System.Data.DataRowVersion.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)(0)), ((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)(0)), ((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("@Original_state", System.Data.SqlDbType.VarChar, 2, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((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>
 /// 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<m_arrHeaderCells.Count; i++)
   {
    TableCell item = (TableCell)m_arrHeaderCells[i];    
    if(item.ColumnSpan > 1)
    {
     iCurrIndex += item.ColumnSpan-1;
    }
    if(item.RowSpan > 1)
    {
     m_htblRowspanIndex.Add(iCurrIndex + i, iCurrIndex + 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 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< ctl.Controls.Count; i++)
   {
    if((null == m_htblRowspanIndex[i]))
    {
     ctl.Controls[i].RenderControl(writer);
    }
   }   
   //*** We don't need to write the </TR> close tag because the writer will do that for us
   //   and so we're done :)
  }
  /// <summary>
  /// 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 our own method
    e.Item.SetRenderMethodDelegate(new RenderMethod(NewRenderMethod));    
   }
  }

  /// <summary>
  /// Hold the reference to the datagrid to decorate
  /// </summary>
  private DataGrid m_dgDatagridToDecorate = null;
  private ArrayList m_arrHeaderCells = null;
  private Hashtable m_htblRowspanIndex = new Hashtable();

 }
}

转自

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

  • 0
    点赞
  • 1
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值