gridview如何动态加入表头

gridview如何动态加入表头,第1张

在dropdownlist事件中添加以下代码

DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn()

col.HeaderText = “AA”

col.Name = "columnt" + i.ToString()

col.SortMode = DataGridViewColumnSortMode.Automatic

this.view_dgv.Columns.Add(col)

1,样式固定

<style type="text/css">

.Freezing

{

position:relative

table-layout:fixed

top:expression(this.offsetParent.scrollTop)

z-index: 10

}

.Freezing th{text-overflow:ellipsisoverflow:hiddenwhite-space: nowrappadding:2px}

</style>

2.Javascript方法

//创建表头

if(document.getElementById("gvTitle") == null)

{

var gdvList = document.getElementById("gvCommon")

var gdvHeader = gdvList.cloneNode(true)

for(i = gdvHeader.rows.length - 1i >0i--)

{

gdvHeader.deleteRow(i)

}

document.getElementById("divTitle").appendChild(gdvHeader)

gdvList.deleteRow(0)

//gdvList.rows[0].style.display = 'none'

}

大致做法是利用JS方法Copy出一个表头 gdvHeader 放在一个“divTitle”的DIV中。

GridView是包含在“divGvData”DIV中的,然后设置divTitle的页面位置和divGvData的一致,也就是覆盖在上面。目前发现效果还行。有一点要注意,gdvHeader.id = "gvTitle"要重新设置一个ID,不然删除的还是GridView的数据行。

3.HTML中的部分代码:

<div id="divTitle" style="position:relativetop:0left:0overflow:hiddenwidth:978pxborder:0px solid red"></div>

<div id="divGvData" runat="server" style="position:relativetop:0pxleft:0pxoverflow:scrollwidth:994pxheight:450px" onscroll="funGrilViewScroll()return false">

<asp:GridView ID="gvCommon" style="position:relativetop:0pxleft:0px" runat="server" CssClass="gvFixd" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" AutoGenerateColumns="False" GridLines="Vertical" PageSize="5" AllowSorting="True" OnSorting="gvCommon_Sorting" >

<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />

<RowStyle BackColor="#E7E7FF" ForeColor="Black" Font-Size="Small" />

<HeaderStyle HorizontalAlign="Center" BackColor="#000084" BorderColor="White" BorderWidth="1px" BorderStyle="Solid" Font-Bold="True" ForeColor="White"/>

</asp:GridView>

</div>

4.asp.net方法

protected void InitGridviewHeader(GridView _gv1, Table _tb1, Panel _pc1)

{

//Page.EnableViewState = false

//[Espal]Copiando las propiedades del renglon de encabezado

//[English]Coping a header row data and properties

_tb1.Rows.Add(_gv1.HeaderRow)

_tb1.Rows[0].ControlStyle.CopyFrom(_gv1.HeaderStyle)

_tb1.CellPadding = _gv1.CellPadding

_tb1.CellSpacing = _gv1.CellSpacing

_tb1.BorderWidth = _gv1.BorderWidth

//if (!_gv1.Width.IsEmpty)

//_gv1.Width = Unit.Pixel(Convert.ToInt32(_gv1.Width.Value) + Convert.ToInt32(_tb1.Width.Value) + 13)

//[Espa]Copiando las propiedades de cada celda del nuevo encabezado.

//[English]Coping each cells properties to the new header cells properties

int Count = 0

_pc1.Width = Unit.Pixel(100)

for (Count = 0Count <_gv1.HeaderRow.Cells.Count - 1Count++)

{

_tb1.Rows[0].Cells[Count].Width = _gv1.Columns[Count].ItemStyle.Width

_tb1.Rows[0].Cells[Count].BorderWidth = _gv1.Columns[Count].HeaderStyle.BorderWidth

_tb1.Rows[0].Cells[Count].BorderStyle = _gv1.Columns[Count].HeaderStyle.BorderStyle

_pc1.Width = Unit.Pixel(Convert.ToInt32(_tb1.Rows[0].Cells[Count].Width.Value) + Convert.ToInt32(_pc1.Width.Value) + 14)

}

//Panel1.Width = Unit.Pixel(Convert.ToInt32(_tb1.Rows[0].Cells[Count-1].Width.Value) + 12)

}

1.首先要把DevExpress的GridControl转换为BandedGridView

2.设置显示列及绑定的字段

此处设置与我们平常使用GridControl制件一样

3.进入设计视图(见下图),点击Bands,

3.1 添加多个Band,如图中1处按键添加

3.2 布局:拖动相应Band进行布局,根据需要调整Band的位置,

4.3 数据绑定:

【点击Show Columns selector 显示之前我们设置好的列】 选取相应列到指定的Band下面,如图2中区域


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/7957614.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-12
下一篇2023-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存