如何在gridview中设置boundfield的宽度 [英] how to set the width of a boundfield in gridview

查看:1508
本文介绍了如何在gridview中设置boundfield的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在gridview4的boundfield内设置数据字段'highlight''的宽度。表中高亮显示列的大小为varchar(400)。

I am unable to set the width of datafield ''highlights'' inside the boundfield of gridview4. The size of the highlights column in the table is varchar(400).

<asp:GridView ID="GridView4" runat="server"  HorizontalAlign="Center"  AutoGenerateColumns="False"   AllowSorting="true"

                style="border-color:Yellow; font-size:small; top: 3px; left: 2px; position: absolute; height: 42px; width:100px"

                PageSize="5"  allowpaging="true"  onselectedindexchanged="GridView4_SelectedIndexChanged" >
            <RowStyle HorizontalAlign="Center" />
            <Columns >
               <asp:BoundField DataField="highlights" HeaderText="highlights"

                            SortExpression="highlights">
                            <HeaderStyle Width="100px" />
                            <ItemStyle Width="100px" />
                            <ItemStyle Height="67px" />
                            </asp:BoundField>
            </Columns>
             <SelectedRowStyle BackColor="Green" />
            <HeaderStyle BackColor="red" />
            <AlternatingRowStyle BackColor="#99CCFF" HorizontalAlign="Center" />
        </asp:GridView>





运行上面的代码时,属于这个''highlight''列的数据显示在

中单线和延伸到gridview外面。任何人都可以指导我吗?



When the above code is run the data belonging to this ''highlights'' column is displayed in a
single line and stretches outside the gridview. Can anyone guide me?

推荐答案

您可以使用GridView的RowDataBound事件。下面是示例代码。还有一件事,单元格的宽度总是以像素为单位设置(不是字符)



protected void GridView4_RowDataBound(object sender,GridViewRowEventArgs e)

{

//使用列索引初始化cellIndex,其宽度要控制

int cellIndex = 0;

int cellWidth = 100;

if(e.Row.RowType == DataControlRowType.DataRow)

{

e.Row.Cells [cellIndex] .Width = cellWidth ;

}

}
You can use RowDataBound event of GridView for this. Below is the sample code. One more thing, width for cell is always gets set in pixel (not in characters)

protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Initalize cellIndex with column index whose width you want to control
int cellIndex = 0;
int cellWidth = 100;
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[cellIndex].Width = cellWidth;
}
}


这篇关于如何在gridview中设置boundfield的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆