Hi ramco1917,
Note: For this sample i have used below article. For more details refer below article link.
Please refer below sample.
HTML
<asp:GridView ID="gvStudents" runat="server" AutoGenerateColumns="false" OnDataBound="OnDataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="Pass" HeaderText="Pass" />
<asp:BoundField DataField="Fail" HeaderText="Fail" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] { new DataColumn("ID"),
new DataColumn("Marks")});
dt1.Rows.Add("1", "P");
dt1.Rows.Add("2", "F");
dt1.Rows.Add("3", "P");
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[] { new DataColumn("ID"),
new DataColumn("Pass"),
new DataColumn("Fail")});
for (int i = 0; i < dt1.Rows.Count; i++)
{
DataRow dr = dt2.NewRow();
dr["ID"] = dt1.Rows[i]["ID"];
dr["Pass"] = Convert.ToString(dt1.Rows[i]["Marks"]) == "P" ? "P" : "";
dr["Fail"] = Convert.ToString(dt1.Rows[i]["Marks"]) == "F" ? "F" : "";
dt2.Rows.Add(dr);
}
gvStudents.DataSource = dt2;
gvStudents.DataBind();
gvStudents.HeaderRow.Cells[0].Text = "";
}
}
protected void OnDataBound(object sender, EventArgs e)
{
GridViewRow row = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
TableHeaderCell cell = new TableHeaderCell();
cell.Text = "ID";
cell.ColumnSpan = 1;
row.Controls.Add(cell);
cell = new TableHeaderCell();
cell.ColumnSpan = 2;
cell.Text = "Result";
row.Controls.Add(cell);
gvStudents.HeaderRow.Parent.Controls.AddAt(0, row);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn() {
New DataColumn("ID"),
New DataColumn("Marks")})
dt1.Rows.Add("1", "P")
dt1.Rows.Add("2", "F")
dt1.Rows.Add("3", "P")
Dim dt2 As DataTable = New DataTable()
dt2.Columns.AddRange(New DataColumn() {
New DataColumn("ID"),
New DataColumn("Pass"),
New DataColumn("Fail")})
For i As Integer = 0 To dt1.Rows.Count - 1
Dim dr As DataRow = dt2.NewRow()
dr("ID") = dt1.Rows(i)("ID")
dr("Pass") = If(Convert.ToString(dt1.Rows(i)("Marks")) = "P", "P", "")
dr("Fail") = If(Convert.ToString(dt1.Rows(i)("Marks")) = "F", "F", "")
dt2.Rows.Add(dr)
Next
gvStudents.DataSource = dt2
gvStudents.DataBind()
gvStudents.HeaderRow.Cells(0).Text = ""
End If
End Sub
Protected Sub OnDataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim row As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
Dim cell As TableHeaderCell = New TableHeaderCell()
cell.Text = "ID"
cell.ColumnSpan = 1
row.Controls.Add(cell)
cell = New TableHeaderCell()
cell.ColumnSpan = 2
cell.Text = "Result"
row.Controls.Add(cell)
gvStudents.HeaderRow.Parent.Controls.AddAt(0, row)
End Sub
Screenshot