Problem with DatagridView header text middile allignment

Question?

"I've found a lot of answers on how to align the Header Text for a column but when I set the HeaderText to MiddleRight alignment, there is some sort of padding after the Header text that I can't figure out how to get rid of

The code I use to Right Align the column is below:
DataGridView1.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight 'Aligns the Header Text
DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight 'Just aligns the cell contents

I should also mention that I've tried turning EnableHeadersVisualStyles off and that did not solve the problem. I've also tried TopRight and BottomRight with no change."

Answer
There's a space for the up/down arrow for sorting on that column. If you set the "SortMode" to "NotSortable", you will get what you want:

DataGridView1.Columns(1).S
ortMode = DataGridViewColumnSortMode.NotSortable
DataGridView1.Columns(1).HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight 'Aligns the Header Text
DataGridView1.Columns(1).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight 'Just aligns the cell contents

Read more

Read from Excel sheet to Datatable C# code

public static DataTable ReadFromExcel(string fileName)
{
DataTable dt = new DataTable();
try
{
OleDbConnection con = new OleDbConnection(string.Format(@'Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0', fileName));
OleDbDataAdapter da = new OleDbDataAdapter('select * from list', con);
da.Fill(dt);
}
catch (Exception ex)
{
if (ex.Message.Contains('list'))
{
throw new ExcelRangeException();
}
else
{
throw ex;
}
}
return dt;
}

Read more