c# Winform Force DataGridView to add Hyphen after 5th charter

Booney 166 Reputation points
2021-03-05T21:46:37.717+00:00

I am importing an excel file to a DataGridView, if the user don't put a hyphen after the 5th charter I want to programmably do it.
The code below changes all columns to Uppercase, The column to change is the Third column not sure how to do it with hyphen.

this.dataGridView1.Columns[3].DefaultCellStyle.Format = Not sure if something like this would work ? " - ";

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // Check the value of the e.ColumnIndex property if you want to apply this formatting only so some rcolumns.
            // Makes Uppercasr letters in DataGirdView
            if (e.Value != null)
            {
                e.Value = e.Value.ToString().ToUpper();
                e.FormattingApplied = true;


            }
        }
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,824 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,217 questions
0 comments No comments
{count} votes

Accepted answer
  1. Karen Payne MVP 35,031 Reputation points
    2021-03-06T01:17:55.527+00:00

    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.

    74939-versions.png

    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}");  
        }  
    }  
    

0 additional answers

Sort by: Most helpful