Make the first character upper case of a string in Win Form DataGridView C#.NET

So, I have a data grid view in my C# Win Form application, the cells are editable by default, now I want whenever something is typed in those cell the end value should be treated as Upper Case First, so which means If any user types:

*string => String
example => Example
another => Another
chaRactEr => ChaRactEr*

I can do this in my code in the Cell Value Changed event, but when I do this in the Cell Value Changed event and set the value of that cell as the formatted string (Which is required from end-user) the event gets triggered twice. I can't let this happened since there is a database functionality triggering in this event. I have tried capturing the cell value in other event like Cell Leave, Cell Enter and other events, but never can I capture it.

So I need to know, If there is a any property or characteristic of the Data Grid View in C#.NET which would make the first character of the value as upper case? Any alternate suggestion to this would also be really helpful.

You can use this code:

        bool bchange = false;
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
            if (bchange == false)
                bchange = true;
                String oritext = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                String newtext= oritext.First().ToString().ToUpper() + oritext.Substring (1);
                //Update Database

                //Update cell
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = newtext;

                bchange = false; 

First Letter In Uppercase in C#, The code samples are examples of how to make a string's first letter uppercase using String.Substring() method of C# and .NET. The string has its  0 Make the first character upper case of a string in Win Form DataGridView C#.NET May 2 '18

DataGridView has an event 'CellFormatting'. You can go for something like this:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        if (e.Value != null)
            string str = (string)e.Value;
            str = char.ToUpper(str[0]) + str.Substring(1);
            e.Value = str;
    }, Convert strings to title case. Uppercase first letter. Strings sometimes have lowercase first letters. Uppercasing the first letter is often necessary. The result string  The String.ToUpper() and String.ToLower() methods in C# and .NET convert a string into an uppercase and a lowercase string respectively. These methods are easy to use. The following code sample converts to string into uppercase and lowercase strings.

I can only guess you may be neglecting that when the code "changes" the value in the "cell" that called the CellValueChanged event, will obviously "fire" the CellValueChanged event again when its value is changed to have an upper case string!

To avoid this circular reference, simply turn the event "off" (before you change the cells value), change the cells value… the event will not fire… then turn the event back "on" after the value has changed.

Example; Below checks to see if the cell changed is in column 0, changes the string in the cell to make the first character an upper case character. The code utilizes a text box on the form that will contain text indicating when the CellValueChanged event is fired. If the code runs with the commented code as posted, the text box will contain two (2) entries every time a cell value in column one changes. UN-commenting the two lines of code will show the text box entry will have only one (1) entry. Sandwich the line of code that "changes" the cells value between the line of code that turns the event "off" and the line of code that turns it back "on". Hope this makes sense.

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) {
  if (e.RowIndex >= 0 && e.ColumnIndex >= 0) {
    if (e.ColumnIndex == 0 && dataGridView1.Rows[e.RowIndex].Cells[0].Value != null) {
      string currentData = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
      string newData = char.ToUpper(currentData[0]) + currentData.Substring(1);
      //dataGridView1.CellValueChanged -= new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
      dataGridView1.Rows[e.RowIndex].Cells[0].Value = newData;
      //dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);
      textBox3.Text += "CellValueChanged fired!" + Environment.NewLine;

C# Uppercase First Letter, I'm in the phase of converting from 6 to Net and it seems that even To format a DataGridView column, I think you're supposed to first set a I can't find out what formatting string(s) to use to force upper or So far, the Universe is winning. text to upper case -- do you know what it is? Force Upper in C#. C# - How to make a Textbox Accepts Only Numbers Or Only Characters In C# [with source code] - Duration: 4:32. 1BestCsharp blog 58,815 views

How to force upper case in a DataGridView column?, Function GetFormattedName AS String dim FormattedName as string 'Append last name in the correct format as above FormattedName This code will change all first letters to upper case (ProperCase) as soon as text is put into your textboxes. how to make the print window appear using 2010? This is a Small example for textbox text handling, and number handling.. Make Login and Register Form Step by Step Using NetBeans And MySQL How to Count Characters Length in a Textbox, Convert Upper Case to Lower Case And Lower Case to Upper Case in C# Textbox(Simple Code)Duration: 3:56 Posted: Jan 22, 2017 To format a DataGridView column, I think you're supposed to first set a cell style, then apply that cell style to the column's CellTemplate property. The cell style has a format property, which I think takes the same type of formatting strings as the Format function. But, as I said, I can't find out what formatting string(s) to use to force

Convert Upper Case to Lower Case And Lower Case to , In C#, the Toupper() function of the char class converts a character into uppercase. In the case that we will be discussing, only the first character of the string needs to be converted to uppercase; the rest of the string will stay as it is. svg viewer. The first character How to get a character from a string in Java. Related Courses. Now I will explain how to convert string / text to title case / proper case, upper case and lower case in using c#, with example. To convert text to proper case, upper and lower case in open your aspx page and write the following code

  • When your code in the Cell Value Changed event makes a formatting change, set a flag and skip the database code. When the second trigger comes along, detect the flag, reset the flag and execute the database code.
  • Another option, subclass the right classes so values are always set with upper case first. Here is the source code of DataGridView:
  • Have you tried to turn the CellValueChanged event "off" "before" you change the value (which is obviously triggering it again), the turn it back "on" "after" you change the value?
  • @JoelHarkes I am trying not to depend on some flag.
  • @JohnG do you want me to turn off/on the event by some bool flag? Or is there a default command or code to turn off/on any event?
  • I really appreciate your reply. :-) I am trying not to depend on some flag. My main query was, Is there a default property of DataGridView in .NET which would make the first character of a cell to Upper Case?
  • DataGridView not exist this property. you can also use : this.dataGridView1.CellValueChanged -= new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged); to disable event in CellValueChanged. this.dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellValueChanged); to enable event in CellDoubleClick or ....
  • Does this gets called before the CellValueChanged or after that event?