See if this will work for you, in this case hard-wired to column 1 which in this case is named Column2. May need some adjustments.
Written with .NET Core so the is null will need to change if using .NET Framework rather than .NET Core Framework.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
dataGridView1.CellFormatting += DataGridView1OnCellFormatting;
}
private void DataGridView1OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].Name != "Column2") return;
var currentValue = e.Value?.ToString();
if (currentValue == null || currentValue.Length <= 4) return;
currentValue = currentValue.Replace("-", "");
currentValue = Regex.Replace(currentValue.Insert(5, "-"), @"\-+", "-");
e.Value = currentValue.ToUpper().TrimEnd('-').TrimStart('-');
e.FormattingApplied = true;
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.Rows.Add(new object[] {"Testing","wwwqqeee" });
}
private void button1_Click(object sender, EventArgs e)
{
if (dataGridView1.CurrentRow is null || dataGridView1.CurrentRow.IsNewRow)
{
return;
}
MessageBox.Show($"{dataGridView1.CurrentRow.Cells["Column2"].Value}");
}
}