Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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:
XML
<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>
Posted
Updated 12-Jul-22 4:35am
v2
Comments
Maciej Los 25-Aug-15 2:37am    
Why do you want to set constant width for gridview columns? Do not force user to change screen size, resolution, etc.!

C#
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");

        }
}
 
Share this answer
 
Comments
Sumon562 25-Aug-15 1:18am    
it is not working. Please give me a better solution.
check this once.

http://forums.asp.net/t/1344016.aspx?How+do+I+fix+the+gridview+column+width+
 
Share this answer
 
v2
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.
 
Share this answer
 
Please use % instead. Ex. HeaderStyle Width="10%" ItemStyle Width="10%", like this for all the columns you have.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900