gridview 表头如何居中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了gridview 表头如何居中相关的知识,希望对你有一定的参考价值。

已经设置了,属性HeaderStyle为CENTER,仍然不居中,代码如下
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding ="4">
<Columns >
<asp:BoundField DataField="Title" HeaderText="123123">

</asp:BoundField>
<asp:BoundField DataField="D" HeaderText="1">
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>

</Columns>

</asp:GridView>
求解决办法,就是在GRIDVIEW属性里设计的CENTER
用了你们的方法还是不行啊,郁闷。。

vs 2008
Private Sub tableHeadSet()

\'/* set the table head */
Dim tcHeader As TableCellCollection = GridView1.HeaderRow.Cells

\'第一行表头
tcHeader.Clear()
tcHeader.Add(New TableHeaderCell())
tcHeader(0).BackColor = Drawing.Color.DarkBlue
tcHeader(0).ForeColor = Drawing.Color.White
tcHeader(0).Height = 30
tcHeader(0).Attributes.Add("colspan", "16") \'跨Column
\'tcHeader(0).Font.Size = 9
\'tcHeader(0).Font.Bold = False
tcHeader(0).Text = "采油井参数显示</th></tr><tr>"
\'第二行表头
tcHeader.Add(New TableHeaderCell())
tcHeader(1).BackColor = Drawing.Color.DarkMagenta
tcHeader(1).ForeColor = Drawing.Color.White
tcHeader(1).HorizontalAlign = HorizontalAlign.Center
tcHeader(1).Width = 150
tcHeader(1).Text = "油井名称"
tcHeader.Add(New TableHeaderCell())
tcHeader(2).BackColor = Drawing.Color.DarkGreen
tcHeader(2).ForeColor = Drawing.Color.White
tcHeader(2).HorizontalAlign = HorizontalAlign.Center
tcHeader(2).Width = 140
tcHeader(2).Text = "巡井时间"
tcHeader.Add(New TableHeaderCell())
tcHeader(3).BackColor = Drawing.Color.DarkMagenta
tcHeader(3).ForeColor = Drawing.Color.White
tcHeader(3).HorizontalAlign = HorizontalAlign.Center
tcHeader(3).Width = 80
tcHeader(3).Text = "油井状态"
tcHeader.Add(New TableHeaderCell())
tcHeader(4).BackColor = Drawing.Color.DarkGreen
tcHeader(4).HorizontalAlign = HorizontalAlign.Center
tcHeader(4).ForeColor = Drawing.Color.White
tcHeader(4).Width = 70
tcHeader(4).Text = "冲程(m)"
tcHeader.Add(New TableHeaderCell())
tcHeader(5).BackColor = Drawing.Color.DarkMagenta
tcHeader(5).ForeColor = Drawing.Color.White
tcHeader(5).HorizontalAlign = HorizontalAlign.Center
tcHeader(5).Width = 90
tcHeader(5).Text = "冲次(n/M)"
tcHeader.Add(New TableHeaderCell())
tcHeader(6).BackColor = Drawing.Color.DarkGreen
tcHeader(6).ForeColor = Drawing.Color.White
tcHeader(6).HorizontalAlign = HorizontalAlign.Center
tcHeader(6).Width = 80
tcHeader(6).Text = "最大载荷(KN)"
tcHeader.Add(New TableHeaderCell())
tcHeader(7).BackColor = Drawing.Color.DarkMagenta
tcHeader(7).ForeColor = Drawing.Color.White
tcHeader(7).HorizontalAlign = HorizontalAlign.Center
tcHeader(7).Width = 80
tcHeader(7).Text = "最小载荷(KN)"
tcHeader.Add(New TableHeaderCell())
tcHeader(8).BackColor = Drawing.Color.DarkGreen
tcHeader(8).ForeColor = Drawing.Color.White
tcHeader(8).HorizontalAlign = HorizontalAlign.Center
tcHeader(8).Width = 70
tcHeader(8).Text = "A相最大电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(9).BackColor = Drawing.Color.DarkMagenta
tcHeader(9).ForeColor = Drawing.Color.White
tcHeader(9).HorizontalAlign = HorizontalAlign.Center
tcHeader(9).Width = 70
tcHeader(9).Text = "A相最小电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(10).BackColor = Drawing.Color.DarkGreen
tcHeader(10).ForeColor = Drawing.Color.White
tcHeader(10).HorizontalAlign = HorizontalAlign.Center
tcHeader(10).Width = 70
tcHeader(10).Text = "Uab电压(V)"
tcHeader.Add(New TableHeaderCell())
tcHeader(11).BackColor = Drawing.Color.DarkMagenta
tcHeader(11).ForeColor = Drawing.Color.White
tcHeader(11).HorizontalAlign = HorizontalAlign.Center
tcHeader(11).Width = 70
tcHeader(11).Text = "Ubc电压(V)"
tcHeader.Add(New TableHeaderCell())
tcHeader(12).BackColor = Drawing.Color.DarkGreen
tcHeader(12).ForeColor = Drawing.Color.White
tcHeader(12).HorizontalAlign = HorizontalAlign.Center
tcHeader(12).Width = 70
tcHeader(12).Text = "Uca电压(V)"
tcHeader.Add(New TableHeaderCell())
tcHeader(13).BackColor = Drawing.Color.DarkMagenta
tcHeader(13).ForeColor = Drawing.Color.White
tcHeader(13).HorizontalAlign = HorizontalAlign.Center
tcHeader(13).Width = 70
tcHeader(13).Text = "A相电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(14).BackColor = Drawing.Color.DarkGreen
tcHeader(14).ForeColor = Drawing.Color.White
tcHeader(14).HorizontalAlign = HorizontalAlign.Center
tcHeader(14).Width = 70
tcHeader(14).Text = "B相电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(15).BackColor = Drawing.Color.DarkMagenta
tcHeader(15).ForeColor = Drawing.Color.White
tcHeader(15).HorizontalAlign = HorizontalAlign.Center
tcHeader(15).Width = 70
tcHeader(15).Text = "C相电流(A)"
tcHeader.Add(New TableHeaderCell())
tcHeader(16).BackColor = Drawing.Color.DarkGreen
tcHeader(16).ForeColor = Drawing.Color.White
tcHeader(16).HorizontalAlign = HorizontalAlign.Center
tcHeader(16).Width = 80
tcHeader(16).Text = "井口压力(Mpa)</th></tr><tr>"
End Sub

要想使表头字体居中,要注意表头各列列宽,表头总列宽,才可以达到完美。
GridView1.Width = 1400
参考技术A 居中直接一句align就可以居中,但是如果想实现表头居中,内容靠左的话(靠右修改为"text-align:right;),可以用这种方式实现:
align : 'center',
formatter: function(value,row,index)
if(value!=null)
return '<div style="text-align:left;width:100%">'+value+"</div>";

,
相当于在datagrid封装好的居中div层里面再封装一层,就可以实现了
参考技术B 表头是默认居中的,你的代码明明是要让行里面的内容居中,把<HeaderStyle HorizontalAlign="Center" />换成<ItemStyle HorizontalAlign="Center" />就可以居中显示了,记得 追加。。。哈哈 参考技术C 你可以将此列转换为模板列 即转换成templateField后再将 controlStyle与HeaderStyle中的宽度设定为50px,行剧中后再试试! 参考技术D <HeaderStyle HorizontalAlign="Center" />加这个在 </Columns></asp:GridView>之间

如何让GridView 标题居中

参考技术A <asp:BoundField DataField="ID" >
<ItemStyle Horizontalalign="Center"/>
</asp:BoundField>本回答被提问者和网友采纳

以上是关于gridview 表头如何居中的主要内容,如果未能解决你的问题,请参考以下文章

如何设置winform中gridview的表头宽度,及编号

c#winfrom中gridview控件怎样设置让表头不可点击,还有下面点击全选中,还不是点击只显示一个单元格,求助,谢

QtableWidget表格中添加控件怎么居中

如何让Gridview在没有数据的时候显示表头

很急GridView固定表头和指定列

grid表头能换行显示吗