Hello,
I am attempting to align the contents of GridView cells on ASP.NET Web Forms, but I am encountering an issue where the GridView header is not aligning as expected. Here is how it looks in the image below
What steps can be taken to resolve this, please?
Here is the code I have tried:
HTML
<asp:GridView ID="Gridview2" runat="server" CellPadding="10" RowStyle-Width="100%" RowStyle-CssClass="table" CssClass="table table-condensed" Font-Size="10pt" HeaderStyle-Width="100%" HeaderStyle-Font-Bold="true" HeaderStyle-Font-Size="10pt" AutoGenerateColumns="False" Style="width: 100%; font-weight: 400;" RowStyle-Height="50px"
BorderStyle="None" GridLines="None" Height="70px" HeaderStyle-Height="50px" HeaderStyle-BackColor="#F9FBFC" 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>
C#
protected void Page_Load(object sender, EventArgs e)
{
GetData();
}
private void GetData()
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4] {
new DataColumn("Item", typeof(string)),
new DataColumn("Qty", typeof(string)),
new DataColumn("Rate", typeof(string)),
new DataColumn("Amount",typeof(string)) });
dt.Rows.Add("Flight Ticket", "1", "132,000.00", "132,000.00");
dt.Rows.Add("Service Charge", "1", "3,000.00", "3,000.00");
dt.Rows.Add("Online Fees", "1", "1,000.00", "1,000.00");
Gridview2.DataSource = dt;
Gridview2.DataBind();
}
}
protected void Gridview1_RowDataBound (object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].HorizontalAlign = HorizontalAlign.Center;
e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right;
e.Row.Cells[3].HorizontalAlign = HorizontalAlign.Right;
}
}