Click here to Skip to main content
14,733,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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"

                            <HeaderStyle Width="100px" />
                            <ItemStyle Width="100px" />
                            <ItemStyle Height="67px" />
             <SelectedRowStyle BackColor="Green" />
            <HeaderStyle BackColor="red" />
            <AlternatingRowStyle BackColor="#99CCFF" HorizontalAlign="Center" />

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?
kumar2413 21-May-13 5:08am
try to have a look in this link
Thanks7872 21-May-13 5:31am
Edit the comment as it contains wrong link.

1 solution

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;

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