No it is not possible to add line break on DataTable column header.
While displaying like GridView, you can add line break.
Refer below example.
HTML
<asp:GridView ID="gvPivot" runat="server" OnRowCreated="OnRowCreated"></asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("test_name");
dt.Columns.Add("test_weight", typeof(int));
dt.Columns.Add("matrik", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("Final", typeof(int));
dt.Columns.Add("Grade", typeof(string));
dt.Columns.Add("Markah", typeof(int));
dt.Rows.Add("name1", "100", "50", "John", "150", "A+", "75");
dt.Rows.Add("name3", "100", "50", "John", "100", "A", "75");
dt.Rows.Add("name2", "100", "75", "Robert", "200", "E", "75");
gvPivot.DataSource = this.PivotData(dt);
gvPivot.DataBind();
}
}
protected void OnRowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace(" (", "<br />");
}
}
}
private DataTable PivotData(DataTable sourceTable)
{
DataTable pivotTable = new DataTable();
pivotTable.Columns.Add("matrik", typeof(int));
pivotTable.Columns.Add("name", typeof(string));
// Extract unique TestName and TestWeight pairs and create columns for them
var testNameWeights = sourceTable.AsEnumerable()
.Select(row => new
{
TestName = row.Field<string>("test_name"),
TestWeight = row.Field<int>("test_weight")
})
.Distinct()
.ToList();
foreach (var item in testNameWeights)
{
string columnName = $"{item.TestName} ({item.TestWeight})";
pivotTable.Columns.Add(columnName, typeof(int));
}
pivotTable.Columns.Add("Final", typeof(int));
pivotTable.Columns.Add("Grade", typeof(string));
// Group data by MatricNo and Name
var groupedData = sourceTable.AsEnumerable()
.GroupBy(row => new
{
MatricNo = row.Field<int>("matrik"),
Name = row.Field<string>("name"),
Final = row.Field<int>("Final"),
Grade = row.Field<string>("Grade")
});
// Populate pivot table
foreach (var group in groupedData)
{
DataRow newRow = pivotTable.NewRow();
newRow["matrik"] = group.Key.MatricNo;
newRow["name"] = group.Key.Name;
newRow["Final"] = group.Key.Final;
newRow["Grade"] = group.Key.Grade;
foreach (var row in group)
{
string testName = row.Field<string>("test_name");
int testWeight = row.Field<int>("test_weight");
string columnName = $"{testName} ({testWeight})";
newRow[columnName] = row.Field<int>("Markah");
}
pivotTable.Rows.Add(newRow);
}
return pivotTable;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("test_name")
dt.Columns.Add("test_weight", GetType(Integer))
dt.Columns.Add("matrik", GetType(Integer))
dt.Columns.Add("name", GetType(String))
dt.Columns.Add("Final", GetType(Integer))
dt.Columns.Add("Grade", GetType(String))
dt.Columns.Add("Markah", GetType(Integer))
dt.Rows.Add("name1", "100", "50", "John", "150", "A+", "75")
dt.Rows.Add("name3", "100", "50", "John", "100", "A", "75")
dt.Rows.Add("name2", "100", "75", "Robert", "200", "E", "75")
gvPivot.DataSource = Me.PivotData(dt)
gvPivot.DataBind()
End If
End Sub
Protected Sub OnRowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.Header Then
For i As Integer = 0 To e.Row.Cells.Count - 1
e.Row.Cells(i).Text = e.Row.Cells(i).Text.Replace(" (", "<br />")
Next
End If
End Sub
Private Function PivotData(ByVal sourceTable As DataTable) As DataTable
Dim pivotTable As DataTable = New DataTable()
pivotTable.Columns.Add("matrik", GetType(Integer))
pivotTable.Columns.Add("name", GetType(String))
Dim testNameWeights = sourceTable.AsEnumerable().[Select](Function(row) New With {Key
.TestName = row.Field(Of String)("test_name"), Key
.TestWeight = row.Field(Of Integer)("test_weight")
}).Distinct().ToList()
For Each item In testNameWeights
Dim columnName As String = $"{item.TestName} ({item.TestWeight})"
pivotTable.Columns.Add(columnName, GetType(Integer))
Next
pivotTable.Columns.Add("Final", GetType(Integer))
pivotTable.Columns.Add("Grade", GetType(String))
Dim groupedData = sourceTable.AsEnumerable().GroupBy(Function(row) New With {Key
.MatricNo = row.Field(Of Integer)("matrik"), Key
.Name = row.Field(Of String)("name"), Key
.Final = row.Field(Of Integer)("Final"), Key
.Grade = row.Field(Of String)("Grade")
})
For Each group In groupedData
Dim newRow As DataRow = pivotTable.NewRow()
newRow("matrik") = group.Key.MatricNo
newRow("name") = group.Key.Name
newRow("Final") = group.Key.Final
newRow("Grade") = group.Key.Grade
For Each row In group
Dim testName As String = row.Field(Of String)("test_name")
Dim testWeight As Integer = row.Field(Of Integer)("test_weight")
Dim columnName As String = $"{testName} ({testWeight})"
newRow(columnName) = row.Field(Of Integer)("Markah")
Next
pivotTable.Rows.Add(newRow)
Next
Return pivotTable
End Function
creenshot