Hi akhter,
You need to check the GridView row count. If it has row then apply the header code.
Refer below code.
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (GridView1.Rows.Count > 0)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableHeaderCell cell = new TableHeaderCell();
cell.Text = "Action";
cell.ColumnSpan = 1;
cell.Attributes.Add("style", "text-align:center !important;");
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 5;
cell.Text = "Shipper Info";
cell.Attributes.Add("style", "text-align:center !important;");
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 6;
cell.Text = "Invoice Info";
cell.Attributes.Add("style", "text-align:center !important;");
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 5;
cell.Text = "Forwarder Info";
cell.Attributes.Add("style", "text-align:center !important;");
row.Controls.Add(cell);
row.BackColor = System.Drawing.ColorTranslator.FromHtml("#3AC0F2");
row.Height = 50;
GridView1.HeaderRow.Parent.Controls.AddAt(0, row);
}
}