如何修复Gridview列宽 [英] How to fix Gridview column width

查看:74
本文介绍了如何修复Gridview列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RowDataBound事件中修复了Gridview列宽。但有时当数据很大时,列的大小会增加。请帮助我。

I fixed the Gridview column width in RowDataBound event. But sometimes when data is big then the size of the column increase.Please help me.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {

            e.Row.Cells[0].Width = 100;
            e.Row.Cells[1].Width = 170;
            e.Row.Cells[2].Width = 52;
            e.Row.Cells[3].Width = 52;
            e.Row.Cells[4].Width = 60;
        }



源代码:


source code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"

CssClass="mGrid"

onrowdatabound="GridView1_RowDataBound"><Columns>
<asp:BoundField DataField="cMainCategory" HeaderText="Main Category" />
 <asp:BoundField DataField="cDes" HeaderText="Item Description" />
 <asp:BoundField DataField="nCom" HeaderText="Cons." />
 <asp:BoundField DataField="nWst" HeaderText="Wstg" />
 <asp:TemplateField HeaderText="Actual Cons">
 <ItemTemplate>
 <asp:TextBox ID="txtActcons" runat="server" CssClass="textboxforgridview"

MaxLength="10" Width="55px"></asp:TextBox>
<asp:FilteredTextBoxExtender ID="txtActcons_FilteredTextBoxExtender"

runat="server" Enabled="True" TargetControlID="txtActcons" ValidChars=".0123456789">
</asp:FilteredTextBoxExtender>
</ItemTemplate>
</asp:TemplateField>

</Columns>
<HeaderStyle BackColor="#007CB9" ForeColor="White" />
<RowStyle Font-Names="Verdana" Font-Size="10px" />
</asp:GridView>

推荐答案

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {


           e.Row.Cells[0].Width = new Unit("100px");
             e.Row.Cells[1].Width = new Unit("170px");
            e.Row.Cells[2].Width = new Unit("52px");
            e.Row.Cells[3].Width =  new Unit("60px");

        }
}


检查一次。



http://forums.asp.net/t/1344016.aspx?How+do+I+fix+the+gridview+column+width+
check this once.

http://forums.asp.net/t/1344016.aspx?How+do+I+fix+the+gridview+column+width+


请阅读我的评论这个问题。我解释了为什么我不建议使用固定大小的gridview列。



强制相同列宽的一种方法是设置文本长度。看看这里:如何修复gridview列宽度? [ ^ ]



如何:动态设置GridView Web服务器控件列宽 [ ^ ] - 根据数据内容设置列宽 - section。
Please, read my comment to the question. I explained why i do not recommend to use fixed size of gridview columns.

One way to force the same column width is to set text length. Have a look here: How do I fix the gridview column width?[^]

How to: Set GridView Web Server Control Column Width Dynamically[^] - To set column width based on data contents - section.


这篇关于如何修复Gridview列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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