Hey akhter,
Please refer below sample.
HTML
<asp:GridView ID="Gridview1" runat="server" AutoGenerateColumns="true" OnDataBound="Gridview1_DataBound">
</asp:GridView>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = GetDataTable();
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private static DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] { new DataColumn("5-1-2019", typeof(string)), new DataColumn("6-1-2019", typeof(string)), new DataColumn("7-1-2019", typeof(string)), new DataColumn("8-1-2019", typeof(string)) });
dt.Rows.Add("p", "A", "p", "A");
return dt;
}
protected void Gridview1_DataBound(object sender, EventArgs e)
{
DataTable dt = GetDataTable();
for (int i = 0; i < Gridview1.HeaderRow.Cells.Count; i++)
{
for (int j = 0; j < dt.Rows.Count; j++)
{
for (int k = 0; k < dt.Columns.Count; k++)
{
if (Gridview1.HeaderRow.Cells[i].Text == dt.Columns[k].ToString())
{
Gridview1.HeaderRow.Cells[i].Text = Gridview1.HeaderRow.Cells[i].Text.Split('-')[0];
}
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = GetDataTable()
Gridview1.DataSource = dt
Gridview1.DataBind()
End Sub
Private Shared Function GetDataTable() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("5-1-2019", GetType(String)), New DataColumn("6-1-2019", GetType(String)), New DataColumn("7-1-2019", GetType(String)), New DataColumn("8-1-2019", GetType(String))})
dt.Rows.Add("p", "A", "p", "A")
Return dt
End Function
Protected Sub Gridview1_DataBound(ByVal sender As Object, ByVal e As EventArgs)
Dim dt As DataTable = GetDataTable()
For i As Integer = 0 To Gridview1.HeaderRow.Cells.Count - 1
For j As Integer = 0 To dt.Rows.Count - 1
For k As Integer = 0 To dt.Columns.Count - 1
If Gridview1.HeaderRow.Cells(i).Text = dt.Columns(k).ToString() Then
Gridview1.HeaderRow.Cells(i).Text = Gridview1.HeaderRow.Cells(i).Text.Split("-"c)(0)
End If
Next
Next
Next
End Sub
Screenshot