Ok, a few things:
First up, it is clear that you are using bootstrap classes here to format the GridView. (this needs to be pointed out). What occurs then is you are using server code to "try" and set the alignment of the GridView controls (with server side code), and then once the content is rendered, created, and then set to the client side, you ALSO specified some bootstrap classes to THEN format the GridView for you, and that's of course going to override your original settings. More important, you using the GridView formatting attribuotes (such as HorizontalAlign.Right), and that again is not really css, but server side code to spit out some formatted text. Of course, as we all see, THEN specifying a bunch of css classes (from bootstrap) overrides any work we done.
So, it not really JUST adding some references to bootstrap + css that causing the issues here, but that of ALSO specifying the bootstrap table classes here (such as CssClass="table table-condensed" .
Hence, in this example, it looks like boatloads of efforts been made using GridView formatting, and then an whole new extra round of efforts been made using css, which of course is contradicting and overrides all the boatloads of settings you have in code and markup.
However, since css is "cumulative", then I would suggest you attempt to style the columns (alignment) using css, and not the built in GV attributes, since css is going to override such settings anyway.
And since you ARE letting css (table class) to style the GridView (which is really nice, and it also makes the GV responsive for the most part), then I suggest removing all (most) of the GridView formatting you have - it just really not needed, and you can thus clean up the markup you have by boatloads here. No use to have two systems fighting each other - it just going to make formatting kind of "jinky" and not reliable.
So, then, change the formatting using css like this:
protected void Gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
// e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[1].Style.Add("text-align", "right");
e.Row.Cells[2].Style.Add("text-align", "right");
e.Row.Cells[3].Style.Add("text-align", "right");
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Style.Add("text-align", "right");
e.Row.Cells[2].Style.Add("text-align", "right");
e.Row.Cells[3].Style.Add("text-align", "right");
}
}
And the result is this:
Also, since bootstrap does such a good job, then I suggest getting rid of all of the "attempted" formatting (since the css overwrites those settings anyway. Hence you only really need this:
<style>
th {
background-color: lightgray!important;
}
</style>
<asp:GridView ID="Gridview2" runat="server"
CssClass="table table-condensed"
AutoGenerateColumns="False"
width="100%"
OnRowDataBound="Gridview2_RowDataBound" >
<Columns>
<asp:BoundField DataField="Item" HeaderText="Item" />
<asp:BoundField DataField="Qty" HeaderText="Quantity" />
<asp:BoundField DataField="Rate" HeaderText="Price" />
<asp:BoundField DataField="Amount" HeaderText="Amount" />
</Columns>
<HeaderStyle Height="35px" />
</asp:GridView>
And now we get this:
So, HUGE reduction in the markup. And adjust the light grey for the heading if you want something lighter, but all those settings were being overwritten by css anyway, and thus are not required. In fact, even in above, the HeaderStyle tag should be removed (but editing of this post for some reason is not working!).