C# .NET - Cannot align text in GridView columns to left

Asked By Pranay on 18-Oct-12 03:40 PM

<



asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"




DataKeyNames="ProjectId" DataSourceID="SqlDataSource1" Font-Size="Small"




HorizontalAlign="Left">




<AlternatingRowStyle Font-Size="Small" />




<Columns>




<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"




ShowSelectButton="True" />




<asp:BoundField DataField="ProjectId" HeaderText="ProjectId"




InsertVisible="False" ReadOnly="True" SortExpression="ProjectId"




Visible="False" />




<asp:TemplateField HeaderText="Name" SortExpression="Name" ItemStyle-HorizontalAlign="Left">




<EditItemTemplate>




<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>




</EditItemTemplate>




<ItemTemplate>




<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>




</ItemTemplate>




<HeaderStyle HorizontalAlign="Left" />




<ItemStyle HorizontalAlign="Center" Width="150px" />




</asp:TemplateField>




<asp:BoundField DataField="StartDate" DataFormatString="{0:MM/dd/yy}"




HeaderText="Start Date" SortExpression="StartDate">




<HeaderStyle HorizontalAlign="Left" />




<ItemStyle Width="50px" />




</asp:BoundField>




<asp:BoundField DataField="EndDate" DataFormatString="{0:MM/dd/yy}"




HeaderText="End Date" SortExpression="EndDate">




<HeaderStyle HorizontalAlign="Left" />




<ItemStyle HorizontalAlign="Left" Width="50px" />




</asp:BoundField>




<asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes"




Visible="False">




<HeaderStyle HorizontalAlign="Left" />




<ItemStyle HorizontalAlign="Left" />




</asp:BoundField>




<asp:BoundField DataField="Location" HeaderText="Location"




SortExpression="Location">




<HeaderStyle HorizontalAlign="Left" />




<ItemStyle HorizontalAlign="Left" Width="60px" />




</asp:BoundField>




<asp:BoundField DataField="Cost" DataFormatString="{0:c}" HeaderText="Cost"




SortExpression="Cost">




<HeaderStyle HorizontalAlign="Right" />




<ItemStyle HorizontalAlign="Right" Width="60px" />




</asp:BoundField>




<asp:BoundField DataField="LevelOfPriority" HeaderText="Priority"




SortExpression="LevelOfPriority">




<HeaderStyle HorizontalAlign="Left" />




<ItemStyle HorizontalAlign="Left" Width="50px" />




</asp:BoundField>




<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status">




<HeaderStyle HorizontalAlign="Left" />




<ItemStyle HorizontalAlign="Left" Width="50px" />




</asp:BoundField>




<asp:BoundField DataField="RegistrationId" HeaderText="RegistrationId"




SortExpression="RegistrationId" Visible="False" />




<asp:BoundField DataField="Description" HeaderText="Description"




SortExpression="Description">




<HeaderStyle HorizontalAlign="Left" />




<ItemStyle HorizontalAlign="Left" Width="300px" />




</asp:BoundField>




</Columns>




<EditRowStyle Font-Size="Small" />




<EmptyDataRowStyle Font-Size="Small" />




<HeaderStyle Font-Size="Small" />




<PagerStyle HorizontalAlign="Left" />




<RowStyle HorizontalAlign="Left" />




</asp:GridView>


Danasegarane Arunachalam replied to Pranay on 19-Oct-12 02:09 AM
1. Create one css for the textbox text to align as

.AlignLeft
{
  text
-align:left;
  font
-family:Verdana, Arial, Helvetica, sans-serif;
}

2. Then apply the css to the Textbox as

<asp:TextBox ID="TextBox1" runat="server" CssClass="AlignLeft" Text='<%# Bind("Name") %>'></asp:TextBox>
Pranay replied to Danasegarane Arunachalam on 19-Oct-12 11:19 AM
It does work but for some reason it is only taking "text align: left".  This did not change any visual effect because label was small compared to column so I put "width:150px;" in the css file but for some reason it won't  change the size. I set the width in the control itself and then it worked. Due you know why this is happening?
Danasegarane Arunachalam replied to Pranay on 22-Oct-12 02:47 AM
Setting width in CSS should work. Try clearing the cache of the Browser.