As you are creating CSV file so you cannot make any modification to word by assigning it height or making it bold. In csv it will just add the text format only which separated each cell value as separating by comma ( , ) so it will also not maintain any merging option of cell like we do in excel file. You can just do it by adding empty string before your heading content so it will just move to particular position in csv file.
Refer the below sample code for your understanding.
Form Design
C#
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.BindDataGridView();
}
private void BindDataGridView()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
this.dataGridView1.DataSource = dt;
}
private void btnExport_Click(object sender, EventArgs e)
{
//Build the CSV file data as a Comma separated string.
string csv = string.Empty;
//Add your Heading With empty string seprated by comma to make space of cell from begining
csv += "," + "," + "User Details" + "," + "\r\n" + "\r\n";
csv += "" + "," + "\r\n";
csv += "" + "," + "\r\n";
csv += "" + "," + "\r\n";
//Add the Header row for CSV file.
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
csv += column.HeaderText + ',';
}
//Add new line.
csv += "\r\n";
//Adding the Rows
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
//Add the Data rows.
csv += cell.Value.ToString().Replace(",", ";") + ',';
}
//Add new line.
csv += "\r\n";
}
//Exporting to CSV.
string folderPath = "D:\\CSV\\";
File.WriteAllText(folderPath + "DataGridViewExport.csv", csv);
}
}
VB.Net
Public Class Form1
Public Sub New()
InitializeComponent()
Me.BindDataGridView()
End Sub
Private Sub BindDataGridView()
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Me.dataGridView1.DataSource = dt
End Sub
Private Sub btnExport_Click(sender As System.Object, e As System.EventArgs) Handles btnExport.Click
'Build the CSV file data as a Comma separated string.
Dim csv As String = String.Empty
'Add your Heading With empty string seprated by comma to make space of cell from begining
csv += ","c & ","c & "User Details" & ","c & vbCr & vbLf
csv += "" & ","c & vbCr & vbLf
csv += "" & ","c & vbCr & vbLf
csv += "" & ","c & vbCr & vbLf
'Add the Header row for CSV file.
For Each column As DataGridViewColumn In dataGridView1.Columns
csv += column.HeaderText & ","c
Next
'Add new line.
csv += vbCr & vbLf
'Adding the Rows
For Each row As DataGridViewRow In dataGridView1.Rows
For Each cell As DataGridViewCell In row.Cells
'Add the Data rows.
csv += cell.Value.ToString().Replace(",", ";") & ","c
Next
'Add new line.
csv += vbCr & vbLf
Next
'Exporting to Excel
Dim folderPath As String = "D:\CSV\"
File.WriteAllText(folderPath & "DataGridViewExport.csv", csv)
End Sub
End Class
Screenshot