I have to make the gridview using C# and ASP.NET. The gridview shown below get data from this SQL Server 2019 table dbfiddle
I have searched numerous times both on google and in Stackoverflow threads and I have written code; however, I cannot customize as per the attached screenshot.
I tried my code without success because this is the output from gridview (please do not look at the numbers in this table, because the data has been updated since the initial image... thanks):
How do I resolve this?
This is my code:
protected void OnDataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableHeaderCell cell = new TableHeaderCell();
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "Hour";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 1;
cell.Text = "C_CO";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.Text = "710";
cell.ColumnSpan = 3;
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.Text = "810";
cell.ColumnSpan = 3;
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 3;
cell.Text = "830";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 3;
cell.Text = "E10";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 3;
cell.Text = "E40";
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 3;
cell.Text = "TOTAL";
row.Controls.Add(cell);
row.BackColor = ColorTranslator.FromHtml("#3AC0F2");
GridView1.HeaderRow.Parent.Controls.AddAt(0, row);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
GridView1.DataSource = RetrieveProducts();
GridView1.DataBind();
}
private DataTable RetrieveProducts()
{
DataTable dt = new DataTable();
DataSet ds = new DataSet();
using (SqlConnection myConnectionString =
new SqlConnection(ConfigurationManager.ConnectionStrings["ConnSqlServer"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("sp_C_CO", myConnectionString))
{
cmd.CommandTimeout = 2147483;
cmd.Connection.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Clear();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
if (ds.Tables.Count > 0)
{
dt = ds.Tables[0];
}
}
}
return dt;
}
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#9AD6ED" HeaderStyle-ForeColor="#636363"
runat="server" AutoGenerateColumns="false" OnDataBound="OnDataBound">
<Columns>
<asp:BoundField DataField="xHour" HeaderText="" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150" HtmlEncode="false" />
<asp:BoundField DataField="xDate" HeaderText="DateHour" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150" />
<asp:BoundField DataField="Total" HeaderText="Total" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150" HtmlEncode="false" />
<asp:BoundField DataField="A_RG" HeaderText="A_RG" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150" HtmlEncode="false" />
<asp:BoundField DataField="B_RG" HeaderText="B_RG" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150" HtmlEncode="false" />
<asp:BoundField DataField="c_RG" HeaderText="C_RG" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="150" HtmlEncode="false" />
</Columns>
</asp:GridView>


Columnsand you can use whatever control you want, including further grid views. Here's an earlier question related to this stackoverflow.com/questions/62285354/… . Is this helpful for you?