2
votes

My code is as below:

<asp:BoundField DataField = "resultId" HeaderText = "Result Id" ItemStyle-Width ="15%" ControlStyle-Width="300px"/>

I have tried both ItemStyle-Width and ControlStyle-Width to fix my gridView column width but none seem to work.

How do I set the column width ?

6
What is your actual pixel size? You want 15% of 1280px or 300px? - SelvaS
Try setting the property RowStyle-Wrap of the GridView to True. With <RowStyle Width="15%" /> - Mahesh

6 Answers

1
votes
<asp:BoundField DataField = "resultId" HeaderText = "Result Id">
   <ItemStyle Width="30px"></ItemStyle>
</asp:BoundField>
1
votes

Do something like this,

<asp:BoundField DataField = "resultId" HeaderText = "Result Id" HeaderStyle-Width="100px" />
1
votes

First of all you should add autogeneratecolumns="false" to your <asp:GridView>

Your aspx files should look like this:

     <asp:GridView ID="GridView1" CssClass="gridview" autogeneratecolumns="False" Width="900" runat="server" HeaderStyle-CssClass="HeaderStyle">
        <Columns>
            <asp:boundfield DataField="field0">
                <HeaderStyle CssClass="HeaderStyle" />
                <ItemStyle CssClass="ItemStyle" />
                <FooterStyle CssClass="HeaderStyle" />
                <ControlStyle CssClass="HeaderStyle" />
           </asp:boundfield>
            <asp:boundfield DataField="field1">
                <HeaderStyle CssClass="HeaderStyle" />
                <ItemStyle CssClass="ItemStyle" />
                <FooterStyle CssClass="HeaderStyle" />
                <ControlStyle CssClass="HeaderStyle" />
           </asp:boundfield>
           .........
        </Columns>
        </asp:GridView>

Your CSS file may be like this:

    .HeaderStyle
        {
            background-color: #900;
            color: White;
            font-weight: bold;
            white-space:nowrap;
            width: 100px;
            max-width: 100px;
            overflow: hidden;
        }

        .ItemStyle
        {
            background-color: #eaebec;
            color: Black;
            font-weight: bold;
            white-space:nowrap;
            width: 100px;
            max-width: 100px;
            overflow: hidden;
        }
.gridview {
    font-family: Arial, Helvetica, sans-serif;
    color: #666;
    font-size: 12px;
    background: #eaebec;
    margin: 20px;
    border: #ccc 1px solid;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    -moz-box-shadow: 0 1px 2px #d1d1d1;
    -webkit-box-shadow: 0 1px 2px #d1d1d1;
}

You asked this question for c# but I will add VB codes here, however you can convert it:

    Dim tablo As New DataTable
    Dim row As DataRow
    tablo.Columns.Add("field0", System.Type.GetType("System.String"))
    tablo.Columns.Add("field1", System.Type.GetType("System.String"))
    .......
    For i = 1 to XX
        row("field" + Trim(Str(i))) = "Something"
    Next
    tablo.Rows.Add(row)
    GridView1.Columns(0).HeaderText = "Something Header"
    GridView1.Columns(1).HeaderText = "Something Other Header"
    .......

I think that's all. Hope it helps.

1
votes

In the page load function put this code:

protected void Page_Load(object sender, EventArgs e)
{
    GridView1.Attributes.Add("style", "word-break:break-all; word-wrap:break-word");    
}

Then in the Boundfield attribute give the property as:

HeaderStyle-Width="100px" 
0
votes

Try this

<asp:BoundField DataField = "resultId" HeaderText = "Result Id" ItemStyle-Width ="15%" ItemStyle-Wrap="false" ControlStyle-Width="300px"/>

You can try this in px also.

-1
votes
<asp:GridView ID="gvDemo2" runat="server" AutoGenerateColumns="false">
            <AlternatingRowStyle Width="60px" />
            <Columns>
                <asp:BoundField DataField="FileId" HeaderText="Name" ItemStyle-Width="100" />
                <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="100" />
                <asp:BoundField DataField="Number" HeaderText="Number" ItemStyle-Width="100" />
                <asp:BoundField DataField="Email" HeaderText="Email" ItemStyle-Width="100" />
                <asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="100" />
                <asp:BoundField DataField="Message" HeaderText="Message" ItemStyle-Width="100" />
            </Columns>
        </asp:GridView>