how to set width for templatefields in gridview in asp.net

Posted by Navalemanoj0405 under Regular Expressions on 4/29/2011 | Points: 10 | Views : 30239 | Status : [Member] | Replies : 5
Dear all

I want to width for every TemplateField in gridview
Is it possible ? If yes please revert
else
please tell me other solution for the same.

thanks..................




Responses

Posted by: Ndebata on: 4/29/2011 [Member] Starter | Points: 25

Up
0
Down
Actually it will take the width of controls inside the template field.

Set the control width as the width for template field,
Priority will be given to the Control inside the Template (specified in style)
else
it will take the width specified in Itemstyle/HeaderStyle/FooterStyle (see bellow)
<asp:TemplateField HeaderText="Status" SortExpression="Status">

<EditItemTemplate>
<asp:DropDownList ID="cmbUserStatus" CssClass="GridEditinglistBox" runat="server"
SelectedIndex='<%# SetSelectedStatusIndex(DataBinder.Eval(Container.DataItem, "Status")) %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# SetStatusText(DataBinder.Eval(Container.DataItem, "Status")) %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="cmbUserStatus" runat="server" CssClass="GridEditinglistBox">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left" Width="70px" />
<ItemStyle HorizontalAlign="Left" Width="70px" />
<FooterStyle HorizontalAlign="Left" Width="70px" />
</asp:TemplateField>





Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Navalemanoj0405 on: 4/29/2011 [Member] Starter | Points: 25

Up
0
Down
thank for quick reply debata.
still not working

please ckeck my code :
below is my TemplateField :


<asp:TemplateField HeaderText="Vehicle No">

<ItemTemplate>
<span style="text-align: center;">
<center>
<%#Eval("VehicleNo") %>
</span></center>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" Width="100px" />
<ItemStyle HorizontalAlign="Left" Width="100px" />
<FooterStyle HorizontalAlign="Left" Width="100px" />
</asp:TemplateField>



Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 4/29/2011 [Member] Starter | Points: 25

Up
0
Down
Hey try to use this (as you are using span you need to set there or instead you can use a label)
<ItemTemplate>

<span style="text-align: center;width:100px ">
<center>
<%#Eval("VehicleNo") %>
</span></center>
</ItemTemplate>

or
<ItemTemplate>

<asp:Label ID="Label1" runat="server" Width="100" Text='<%#Eval("VehicleNo") %>'></asp:Label>
</ItemTemplate>


Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Navalemanoj0405 on: 4/29/2011 [Member] Starter | Points: 25

Up
0
Down
thank U Debata.. it is working

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Srinivas673 on: 4/29/2011 [Member] Starter | Points: 25

Up
0
Down

itemStyle-Width and HeaderStyle-Width in templatefiled if which one is larger take that width

<asp:TemplateField HeaderText="User ID" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="100px"
HeaderStyle-Width="100px" SortExpression="Updateddate">
<ItemTemplate>
<asp:Label ID="lblDateTime" runat="server" Text='<%#Eval("UpdateUser") %>' />
</ItemTemplate>
</asp:TemplateField>

Navalemanoj0405, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response